springmvc 项目单元测试
对于web项目如果希望通过url来进行单元测试,但是启动服务器和建立http client 来进行测试非常麻烦,并且依赖网络环境。这样我们可以通过引入MockMvc进行测试。
一、引入jar包
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path-assert</artifactId> <version>0.8.1</version> </dependency>
二、测试代码
1、dao层和service层
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"})
@TransactionConfiguration(transactionManager="txManager",defaultRollback=true)
@Transactional//应用事务,这样测试就不会在数据库中留下痕迹
public class BaseJunit4Test {
@Test
public void test(){
}
}
public class LoginServiceTest extends BaseJunit4Test{ @Autowired private LoginService loginService; @Test public void testLogin() { String account = "kyle"; String password = "123456"; String result = loginService.Login(account, password); assertEquals("登陆成功",result); } }
public class LoginMapperTest extends BaseJunit4Test{ @Autowired private LoginMapper loginMapper; @Test public void testGetUserPwdByAccount() { String account = "kyle"; String pwd = loginMapper.getUserPwdByAccount(account); assertEquals("123456",pwd); } }
2、web层测试
@RunWith(SpringJUnit4ClassRunner.class)//使用Spring Test组件进行单元测试 @ContextConfiguration(locations={"classpath:applicationContext.xml",//加载配置文件 "classpath:spring-mvc.xml" }) @WebAppConfiguration
@TransactionConfiguration(transactionManager="txManager",defaultRollback=true)
@Transactional//应用事务,这样测试就不会在数据库中留下痕迹
public class BaseWebJunit4Test { protected MockMvc mockMvc; protected MockHttpSession mockHttpSession; @Autowired protected WebApplicationContext context; @Before public void initMockMvc() throws Exception { mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); this.mockHttpSession = new MockHttpSession(); mockMvc.perform(MockMvcRequestBuilders.post("/login") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .param("account", "kyle") .param("password", "123456") .session(mockHttpSession)) .andExpect(status().isOk()) .andExpect(content().string("登陆成功")) .andDo(print()) .andReturn().getResponse().getContentAsString(); } @Test public void test(){ } }
public class LoginControllerTest extends BaseWebJunit4Test{ @Test public void testLogin() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/login") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .param("account", "kyle") .param("password", "123456") .session(mockHttpSession)) .andExpect(status().isOk()) .andExpect(content().string("登陆成功")) .andDo(print()) .andReturn().getResponse().getContentAsString(); } @Test public void testGetUserInfo() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/getUserInfo") .contentType(MediaType.APPLICATION_JSON) .content("{\"account\":\"kyle\"}") .session(mockHttpSession)) .andExpect(status().isOk()) .andExpect(jsonPath("$.password", is("123456"))) .andDo(print()) .andReturn().getResponse().getContentAsString(); } }
三、mock mvc 相关api
https://blog.csdn.net/xiao_xuwen/article/details/52890730