步驟
整個測試過程非常有規律:
- 準備測試環境
- 通過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();
}