为什么在模拟服务时模拟 mvc 测试失败并出现异常?
Why mocked mvc test fails with exception when service is mocked?
我不明白为什么这个测试会异常失败?
服务 class 被嘲笑...
@Autowired
private MockMvc mockMvc;
@MockBean
private InventoryService inventoryService;
private List<InventoryDTO> inventoryList;
@BeforeEach
void setup() {
...
}
@Test
@DisplayName("POST /inventory test - status 200")
@WithMockUser(roles = {"PUBLISHER", "USER"})
void addItem() throws Exception {
doReturn(inventoryList.get(0)).when(inventoryService).add(any());
mockMvc.perform(
MockMvcRequestBuilders.post("/inventory")
.content(asJsonString(inventoryList.get(0)))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().json("{'name':'test1','description':'test-1-description','price':10}"))
.andReturn();
}
这是经过测试的控制器:
@RestController
public class InventoryController {
private final InventoryService inventoryService;
public InventoryController(InventoryService inventoryService) {
this.inventoryService = inventoryService;
}
@GetMapping("/inventory")
@ResponseBody public List<InventoryDTO> allInventory(){
return inventoryService.findAll();
}
@PostMapping("/inventory")
@ResponseBody public InventoryDTO addInventory(@RequestBody InventoryDTO inventoryDTO){
return inventoryService.add(inventoryDTO);
}
例外情况:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.teamcompetencymatrix.www.dto.AuditDTO]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.teamcompetencymatrix.www.dto.AuditDTO` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 72] (through reference chain: com.teamcompetencymatrix.www.dto.InventoryDTO["audit"])
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:72)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
据我所知,mockito 应该模拟服务 class,并且测试不会进入其中...
据我所知,审计员对财务总监没有任何影响class。
错误说明了一切:
Cannot construct instance of
com.teamcompetencymatrix.www.dto.AuditDTO
(no Creators, like default
constructor, exist)
请求主体无法转换为 DTO,请尝试向 AuditDTO
添加一个空的构造函数和 setter
我不明白为什么这个测试会异常失败? 服务 class 被嘲笑...
@Autowired
private MockMvc mockMvc;
@MockBean
private InventoryService inventoryService;
private List<InventoryDTO> inventoryList;
@BeforeEach
void setup() {
...
}
@Test
@DisplayName("POST /inventory test - status 200")
@WithMockUser(roles = {"PUBLISHER", "USER"})
void addItem() throws Exception {
doReturn(inventoryList.get(0)).when(inventoryService).add(any());
mockMvc.perform(
MockMvcRequestBuilders.post("/inventory")
.content(asJsonString(inventoryList.get(0)))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().json("{'name':'test1','description':'test-1-description','price':10}"))
.andReturn();
}
这是经过测试的控制器:
@RestController
public class InventoryController {
private final InventoryService inventoryService;
public InventoryController(InventoryService inventoryService) {
this.inventoryService = inventoryService;
}
@GetMapping("/inventory")
@ResponseBody public List<InventoryDTO> allInventory(){
return inventoryService.findAll();
}
@PostMapping("/inventory")
@ResponseBody public InventoryDTO addInventory(@RequestBody InventoryDTO inventoryDTO){
return inventoryService.add(inventoryDTO);
}
例外情况:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.teamcompetencymatrix.www.dto.AuditDTO]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.teamcompetencymatrix.www.dto.AuditDTO` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 72] (through reference chain: com.teamcompetencymatrix.www.dto.InventoryDTO["audit"])
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:72)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
据我所知,mockito 应该模拟服务 class,并且测试不会进入其中... 据我所知,审计员对财务总监没有任何影响class。
错误说明了一切:
Cannot construct instance of
com.teamcompetencymatrix.www.dto.AuditDTO
(no Creators, like default constructor, exist)
请求主体无法转换为 DTO,请尝试向 AuditDTO