對於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