I have a problem with testing my Spring Boot Controller. I'm trying to test one of Controller put methods, but all I'm getting is Status expected:<200> but was:<400> error message.
My Controller:
@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);
}
My ControllerTest: The URL is ok, and I have a list just like in the get method in my controller.
@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());
}
And here's an error message:
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$9(StatusResultMatchers.java:627)
at org.springframework.test.web.servlet.MockMvc$1.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>
I guess it has to be something with sending the required parameter, but I'm not sure what exactly causes this 400 error.
CodePudding user response:
You do not have content in the request
mockMvc.perform(
MockMvcRequestBuilders.put("/api/update")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(jsonString)
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
CodePudding user response:
For anyone interested, I've managed to find a solution, so here it is.
@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());
}
If you compare this code with the one in my original post, the difference is in adding the ObjectMapper
.