Spring 引导中 GET 请求的 MockMvc 结果 returns null
MockMvc result returns null for GET request in Spring Boot
我在测试 Spring 引导控制器时遇到问题。我正在尝试测试 Controller put 方法之一,但我得到的只是 Status expected:<200> but was:<400> 错误消息。
我的控制器:
@PutMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> updateFields(@RequestBody List<UpdateModel> model) throws JsonProcessingException {
return new ResponseEntity<>(vaultService.updateFields(model), HttpStatus.OK);
}
我的控制器测试:
URL 没问题,我有一个列表,就像我控制器中的 get 方法一样。
@Test
void updateFieldsTest() throws Exception {
List<UpdateModel> response = new ArrayList<>();
UpdateModel updateModel = new UpdateModel();
response.add(updateModel);
when(vaultService.updateFields(anyList()))
.thenReturn(String.valueOf(response));
mockMvc.perform(
MockMvcRequestBuilders.put("/api/update")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
这是一条错误消息:
MockHttpServletRequest:
HTTP Method = PUT
Request URI = /api/update
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json"]
Body = null
Session Attrs = {}
Handler:
Type = hr.ogcs.ltt.api.lttapi.controller.LttController
Method = hr.ogcs.ltt.api.lttapi.controller.LttController#updateFields(List)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.http.converter.HttpMessageNotReadableException
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 400
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 = []
Status expected:<200> but was:<400>
Expected :200
Actual :400
<Click to see difference>
java.lang.AssertionError: Status expected:<200> but was:<400>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher(StatusResultMatchers.java:627)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:212)
at hr.ogcs.ltt.api.apicontroller.ControllerTest.updateFieldsTest(ControllerTest.java:113) <31 internal lines>
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<9 internal lines>
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<48 internal lines>
我猜它必须与发送必需的参数有关,但我不确定究竟是什么导致了这个 400 错误。
您的请求中没有内容
mockMvc.perform(
MockMvcRequestBuilders.put("/api/update")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(jsonString)
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
对于任何感兴趣的人,我已经设法找到了解决方案,所以就在这里。
@Test
@DisplayName("Test updateFieldsTest")
void updateFieldsTest() throws Exception {
List<UpdateModel> response = new ArrayList<>();
UpdateModel updateModel = new UpdateModel();
response.add(updateModel);
when(vaultService.updateFields(anyList()))
.thenReturn(String.valueOf(response));
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(response);
mockMvc.perform(
MockMvcRequestBuilders.put("/api/update")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(json)
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
如果将此代码与我原来的 post 中的代码进行比较,区别在于添加了 ObjectMapper
.
我在测试 Spring 引导控制器时遇到问题。我正在尝试测试 Controller put 方法之一,但我得到的只是 Status expected:<200> but was:<400> 错误消息。
我的控制器:
@PutMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> updateFields(@RequestBody List<UpdateModel> model) throws JsonProcessingException {
return new ResponseEntity<>(vaultService.updateFields(model), HttpStatus.OK);
}
我的控制器测试: URL 没问题,我有一个列表,就像我控制器中的 get 方法一样。
@Test
void updateFieldsTest() throws Exception {
List<UpdateModel> response = new ArrayList<>();
UpdateModel updateModel = new UpdateModel();
response.add(updateModel);
when(vaultService.updateFields(anyList()))
.thenReturn(String.valueOf(response));
mockMvc.perform(
MockMvcRequestBuilders.put("/api/update")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
这是一条错误消息:
MockHttpServletRequest:
HTTP Method = PUT
Request URI = /api/update
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json"]
Body = null
Session Attrs = {}
Handler:
Type = hr.ogcs.ltt.api.lttapi.controller.LttController
Method = hr.ogcs.ltt.api.lttapi.controller.LttController#updateFields(List)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.http.converter.HttpMessageNotReadableException
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 400
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 = []
Status expected:<200> but was:<400>
Expected :200
Actual :400
<Click to see difference>
java.lang.AssertionError: Status expected:<200> but was:<400>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher(StatusResultMatchers.java:627)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:212)
at hr.ogcs.ltt.api.apicontroller.ControllerTest.updateFieldsTest(ControllerTest.java:113) <31 internal lines>
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<9 internal lines>
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<48 internal lines>
我猜它必须与发送必需的参数有关,但我不确定究竟是什么导致了这个 400 错误。
您的请求中没有内容
mockMvc.perform(
MockMvcRequestBuilders.put("/api/update")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(jsonString)
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
对于任何感兴趣的人,我已经设法找到了解决方案,所以就在这里。
@Test
@DisplayName("Test updateFieldsTest")
void updateFieldsTest() throws Exception {
List<UpdateModel> response = new ArrayList<>();
UpdateModel updateModel = new UpdateModel();
response.add(updateModel);
when(vaultService.updateFields(anyList()))
.thenReturn(String.valueOf(response));
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(response);
mockMvc.perform(
MockMvcRequestBuilders.put("/api/update")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(json)
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
如果将此代码与我原来的 post 中的代码进行比较,区别在于添加了 ObjectMapper
.