MockMvc简介
MockMvc实现对Http请求的模拟,可以方便对Controller进行测试,使得测试速度快、不依赖网络环境,而且提供验证的工具,使得请求的验证统一而且很方便
测试环境
jdk 1.8
springboot 2.x
junit4
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
测试代码
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
//SpringBoot1.4版本之前用的是SpringJUnit4ClassRunner.class
@RunWith(SpringRunner.class)
//SpringBoot1.4版本之前用的是@SpringApplicationConfiguration(classes = Application.class)
@SpringBootTest
//测试环境使用,用来表示测试环境使用的ApplicationContext将是WebApplicationContext类型的
@WebAppConfiguration
//@AutoConfigureMockMvc是用于自动配置MockMvc
@AutoConfigureMockMvc
class UserControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private MockMvc mockMvc;
@Before()
public void setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();//建议使用这种
}
@Test
public void addUser() throws Exception{
MvcResult response = mockMvc.perform(MockMvcRequestBuilders.post("/user/adduser")
.contentType(MediaType.APPLICATION_JSON).param("userAccount","killer").param("userPassword",
"123456").param("userTel","133xxxx6657").param("userImage","logo").param("userName","jack").param(
"userNickname","jeff").param("userAcademy","失乐院").param("userClass","石乐志3班").param(
"userStudentNumber","2019212217890").param("userEmail","shilezhi@example.com").param("gender","男")
.param("classId","2")).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();
System.out.println("result:"+response.getResponse().getContentAsString());
}
}
对mockMvc中一些方法的使用描述。
perform() 发送请求,获取请求结果里面的参数可以选择post|get|put|delete
post()里面的“/user/adduser"即为controller接口的对应url。
contentType()中选择数据的传输方式,这里采用的是JSON格式。
prama()中传输的就是键值对,前面为controller接收参数的名称,后面是传输的值。
andExpect()期望浏览器返回的状态码。这里期望返回200。
andDo(print())将返回信息打印出来。
getRespnse().getcontentAsString()请求返回的值。
返回这样即为成功。
必坑指南
网上的一些注解被弃用,测试的注解就按博主的来最好,实体解析为JSON存在问题。(本人是手打的好多prama,后期如果尝试其他好用的会回来更新滴)