步骤
整个测试过程非常有规律:
- 准备测试环境
- 通过MockMvc执行请求
3.1. 添加验证断言
3.2. 添加结果处理器
3.3. 得到MvcResult进行自定义断言/进行下一步的异步请求 - 卸载测试环境
spring提供了mockMvc模块,可以模拟web请求来对controller层进行单元测试
示例:MockMvc
第一步:添加Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
第二步:框架
- Controller代码:
@RestController
@RequestMapping("/")
public class DeptController {
}
- 测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class DeptControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
}
第三步:细节
1、
@GetMapping("fun1")
public List<Dept> fun1() {
List<Dept> deptList = new ArrayList<>();
deptList.add(new Dept(11, "aa", "aaaaaa"));
deptList.add(new Dept(22, "bb", "bbbbbb"));
deptList.add(new Dept(33, "cc", "cccccc"));
deptList.add(new Dept(44, "dd", "dddddd"));
return deptList;
}
测试代码:
@Test
public void fun1() throws Exception {
String mvcResult= mockMvc.perform(get("/fun1"))
.andReturn().getResponse().getContentAsString();
System.out.println("Result === "+mvcResult);
}
2、
@GetMapping("fun21")
public Dept fun21( int deptno){
Dept dept = new Dept();
dept.setDeptno(deptno);
return dept;
}
测试代码:
@Test
public void fun21() throws Exception {
mockMvc.perform((get("/fun21").param("deptno", "1"))).andExpect(status().isOk()).andDo(print());
}
3、
@GetMapping("fun22/{deptno}")
public Dept fun22(@PathVariable int deptno){
Dept dept = new Dept();
dept.setDeptno(deptno);
return dept;
}
测试代码:
@Test
public void fun22() throws Exception {
mockMvc.perform(get("/fun22/6")).andExpect(status().isOk()).andDo(print());
}
4、
@GetMapping("fun3")
public Dept fun3(){
return new Dept(11, "aa", "aaaaaa");
}
测试代码:
@Test
public void fun3() throws Exception {
String mvcResult = mockMvc.perform(get("/fun3"))
.andReturn().getResponse().getContentAsString();
System.out.println("Result === " + mvcResult);
}
5、
@PostMapping("fun4")
public Dept fun4(Dept dept){
dept.setDeptno(8989);
return dept;
}
测试代码:
@Test
public void fun4() throws Exception {//post
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("dname", "sales");
params.add("loc", "china");
String mvcResult= mockMvc.perform(MockMvcRequestBuilders.post("/fun4")
.params(params)).andReturn().getResponse().getContentAsString();
System.out.println("Result === "+mvcResult);
}
6、
@PutMapping("fun5")
public Dept fun5(Dept dept){
dept.setDeptno(8989);
return dept;
}
测试代码:
@Test
public void fun5() throws Exception {//put
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("dname", "sales");
params.add("loc", "china");
String mvcResult= mockMvc.perform(MockMvcRequestBuilders.put("/fun5")
.params(params)).andReturn().getResponse().getContentAsString();
System.out.println("Result === "+mvcResult);
}
7、
@PatchMapping("fun6")
public Dept fun6(Dept dept){
dept.setDeptno(8989);
return dept;
}
测试代码:
@Test
public void fun6() throws Exception {//patch
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("dname", "sales");
params.add("loc", "china");
String mvcResult= mockMvc.perform(MockMvcRequestBuilders.patch("/fun6")
.params(params)).andReturn().getResponse().getContentAsString();
System.out.println("Result === "+mvcResult);
}
8、
@DeleteMapping("fun7/{deptno}")
public Dept fun7(@PathVariable int deptno){
Dept dept = new Dept();
dept.setDeptno(deptno);
return dept;
}
测试代码:
@Test
public void fun7() throws Exception {//delete
mockMvc.perform(MockMvcRequestBuilders.delete("/fun7/6"))
.andReturn();
}