MockMVC JsonPath 响应有空体?
MockMVC JsonPath response has empty body?
测试控制器出现以下错误:java.lang.AssertionError:JSON 路径“$.firstName”处没有值。显然由于某种原因我的回复正文是空的。 Controller 是一个 RestController,我正确地模拟了该服务。有人可以帮助我吗?
测试类:
@WebMvcTest(EmployeeController.class)
class EmployeeControllerTest {
Employee employee = new Employee();
@Autowired
private MockMvc mvc;
@MockBean
private EmployeeServiceImpl service;
@BeforeEach
public void initEmployee() {
employee.setFirstName("John");
employee.setLastName("Doe");
employee.setPlace("xxxx");
employee.setEmployeeTitle(EmployeeTitle.SENIOR_JAVA_DEVELOPER);
employee.setEmployeeId(1L);
}
@Test
public void createEmployeeAPI() throws Exception {
when(service.addNewEmployee(employee)).thenReturn(employee);
mvc.perform(MockMvcRequestBuilders
.post("/employees")
.content(asJsonString(employee))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.firstName").value("John"));
}
请求是正确的,但显然响应的正文是空的(请参阅下面的控制台输出):
MockHttpServletRequest:
HTTP Method = POST
Request URI = /employees
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json", Content-Length:"124"]
Body = {"employeeId":1,"firstName":"John","lastName":"Doe","employeeTitle":"SENIOR_JAVA_DEVELOPER","place":"xxxx","photoURrl":null}
Session Attrs = {}
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
控制器代码:
@RestController
@RequestMapping("/employees")
public class EmployeeController extends ExceptionHandling {
private final EmployeeService employeeService;
@Autowired
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@PostMapping()
public Employee addEmployee(@RequestBody Employee employee) {
return employeeService.addNewEmployee(employee);
}
问题出在这里:
when(service.addNewEmployee(employee)).thenReturn(employee);
Mockito 在内部使用对象 class 的 equals() 方法来比较作为参数传递给该方法的对象与配置的对象。如果 equals() 未被覆盖,则使用 java.lang.Object 的 equals() 仅比较引用,即如果两个变量都指向堆中的同一个对象。在当前示例中,Employe class(可能)没有实现 equals() 方法。当provided expected创建一个新对象时,引用不是一个,所以Mockito会验证失败。
因为employee对象是从json序列化而来的,所以不是同一个对象。
您可以使用 Mockito.any()
或 Mockito.any(Employee.class)
来解决这个问题。
测试控制器出现以下错误:java.lang.AssertionError:JSON 路径“$.firstName”处没有值。显然由于某种原因我的回复正文是空的。 Controller 是一个 RestController,我正确地模拟了该服务。有人可以帮助我吗?
测试类:
@WebMvcTest(EmployeeController.class)
class EmployeeControllerTest {
Employee employee = new Employee();
@Autowired
private MockMvc mvc;
@MockBean
private EmployeeServiceImpl service;
@BeforeEach
public void initEmployee() {
employee.setFirstName("John");
employee.setLastName("Doe");
employee.setPlace("xxxx");
employee.setEmployeeTitle(EmployeeTitle.SENIOR_JAVA_DEVELOPER);
employee.setEmployeeId(1L);
}
@Test
public void createEmployeeAPI() throws Exception {
when(service.addNewEmployee(employee)).thenReturn(employee);
mvc.perform(MockMvcRequestBuilders
.post("/employees")
.content(asJsonString(employee))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.firstName").value("John"));
}
请求是正确的,但显然响应的正文是空的(请参阅下面的控制台输出):
MockHttpServletRequest:
HTTP Method = POST
Request URI = /employees
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json", Content-Length:"124"]
Body = {"employeeId":1,"firstName":"John","lastName":"Doe","employeeTitle":"SENIOR_JAVA_DEVELOPER","place":"xxxx","photoURrl":null}
Session Attrs = {}
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
控制器代码:
@RestController
@RequestMapping("/employees")
public class EmployeeController extends ExceptionHandling {
private final EmployeeService employeeService;
@Autowired
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@PostMapping()
public Employee addEmployee(@RequestBody Employee employee) {
return employeeService.addNewEmployee(employee);
}
问题出在这里:
when(service.addNewEmployee(employee)).thenReturn(employee);
Mockito 在内部使用对象 class 的 equals() 方法来比较作为参数传递给该方法的对象与配置的对象。如果 equals() 未被覆盖,则使用 java.lang.Object 的 equals() 仅比较引用,即如果两个变量都指向堆中的同一个对象。在当前示例中,Employe class(可能)没有实现 equals() 方法。当provided expected创建一个新对象时,引用不是一个,所以Mockito会验证失败。
因为employee对象是从json序列化而来的,所以不是同一个对象。
您可以使用 Mockito.any()
或 Mockito.any(Employee.class)
来解决这个问题。