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>
springmvc考试选择题<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={"l"})
@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 = UserPwdByAccount(account);
assertEquals("123456",pwd);
}
}
  2、web层测试
@RunWith(SpringJUnit4ClassRunner.class)//使⽤Spring Test组件进⾏单元测试
@ContextConfiguration(locations={"l",//加载配置⽂件"l"
})
@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();
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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。