MockMVC类是 Spring MVC 测试框架的一部分,它通过启动一个Servlet容器帮助测试REST控制器。
在这个MockMVC教程中,我们将使用它和Spring boot的WebMvcTest类来执行 Junit 测试用例,该测试用例测试为 Spring boot 2 hateoas 编写的 REST 控制器方法。
Maven依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
一个JUnit测试类来测试Spring MVC控制器请求和响应,我们可以使用下面给出的配置。
@RunWith(SpringRunner.<b>class</b>) @WebMvcTest(EmployeeRESTController.<b>class</b>) <b>public</b> <b>class</b> TestEmployeeRESTController { @Autowired <b>private</b> MockMvc mvc; }
HTTP GET
假设有一个Rest控制器:
@GetMapping(value = <font>"/employees"</font><font>) <b>public</b> EmployeeListVO getAllEmployees() { </font><font><i>//code</i></font><font> } @GetMapping(value = </font><font>"/employees/{id}"</font><font>) <b>public</b> ResponseEntity<EmployeeVO> getEmployeeById (@PathVariable(</font><font>"id"</font><font>) <b>int</b> id) { </font><font><i>//code</i></font><font> } </font>
下面给出了相应的方法测试:
@Autowired <b>private</b> MockMvc mvc; @Test <b>public</b> <b>void</b> getAllEmployeesAPI() throws Exception { mvc.perform( MockMvcRequestBuilders .get(<font>"/employees"</font><font>) .accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath(</font><font>"$.employees"</font><font>).exists()) .andExpect(MockMvcResultMatchers.jsonPath(</font><font>"$.employees<li>.employeeId"</font><font>).isNotEmpty()); } @Test <b>public</b> <b>void</b> getEmployeeByIdAPI() throws Exception { mvc.perform( MockMvcRequestBuilders .get(</font><font>"/employees/{id}"</font><font>, 1) .accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath(</font><font>"$.employeeId"</font><font>).value(1)); } </font>
HTTP POST
案例:
@PostMapping(value = <font>"/employees"</font><font>) <b>public</b> ResponseEntity<EmployeeVO> addEmployee (@Valid @RequestBody EmployeeVO employee) { </font><font><i>//code</i></font><font> <b>return</b> <b>new</b> ResponseEntity<EmployeeVO>(employee, HttpStatus.CREATED); } </font>
对于post json请求的相应spring springmvc测试如下:
@Autowired <b>private</b> MockMvc mvc; @Test <b>public</b> <b>void</b> createEmployeeAPI() throws Exception { mvc.perform( MockMvcRequestBuilders .post(<font>"/employees"</font><font>) .content(asJsonString(<b>new</b> EmployeeVO(<b>null</b>, </font><font>"firstName4"</font><font>, </font><font>"lastName4"</font><font>, </font><font>"email4@mail.com"</font><font>))) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()) .andExpect(MockMvcResultMatchers.jsonPath(</font><font>"$.employeeId"</font><font>).exists()); } <b>public</b> <b>static</b> String asJsonString(<b>final</b> Object obj) { <b>try</b> { <b>return</b> <b>new</b> ObjectMapper().writeValueAsString(obj); } <b>catch</b> (Exception e) { <b>throw</b> <b>new</b> RuntimeException(e); } } </font>
HTTP PUT
HTTP API在控制器中定义为:
@PutMapping(value = <font>"/employees/{id}"</font><font>) <b>public</b> ResponseEntity<EmployeeVO> updateEmployee (@PathVariable(</font><font>"id"</font><font>) <b>int</b> id, @Valid @RequestBody EmployeeVO employee) { </font><font><i>//code</i></font><font> <b>return</b> <b>new</b> ResponseEntity<EmployeeVO>(emp, HttpStatus.OK); } </font>
相应的方法测试是
@Test <b>public</b> <b>void</b> updateEmployeeAPI() throws Exception { mvc.perform( MockMvcRequestBuilders .put(<font>"/employees/{id}"</font><font>, 2) .content(asJsonString(<b>new</b> EmployeeVO(2, </font><font>"firstName2"</font><font>, </font><font>"lastName2"</font><font>, </font><font>"email2@mail.com"</font><font>))) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath(</font><font>"$.firstName"</font><font>).value(</font><font>"firstName2"</font><font>)) .andExpect(MockMvcResultMatchers.jsonPath(</font><font>"$.lastName"</font><font>).value(</font><font>"lastName2"</font><font>)) .andExpect(MockMvcResultMatchers.jsonPath(</font><font>"$.email"</font><font>).value(</font><font>"email2@mail.com"</font><font>)); } </font>
HTTP DELETE:
@DeleteMapping(value = <font>"/employees/{id}"</font><font>) <b>public</b> ResponseEntity<HttpStatus> removeEmployee (@PathVariable(</font><font>"id"</font><font>) <b>int</b> id) { </font><font><i>//code</i></font><font> <b>return</b> <b>new</b> ResponseEntity<HttpStatus>(HttpStatus.ACCEPTED); } </font>
相应的方法测试是:
@Test <b>public</b> <b>void</b> deleteEmployeeAPI() throws Exception { mvc.perform( MockMvcRequestBuilders.delete(<font>"/employees/{id}"</font><font>, 1) ) .andExpect(status().isAccepted()); } </font>