MockMVC -Inhaltstyp nicht akzeptabelJava

Java-Forum
Anonymous
 MockMVC -Inhaltstyp nicht akzeptabel

Post by Anonymous »

Ich habe die folgende @restController-Methode: < /p>

Code: Select all

@PostMapping(value = "/order", consumes = "application/raildex.OrderDTO.v1+json", produces = "application/json")
public ResponseEntity insertOrder(@Valid @RequestBody OrderDTO order) {
try {
log.debug("Incoming order: {}", order);
orderDAO.insert(order);
return ResponseEntity.ok(new GenericResponseDTO("Successfully inserted order"));
} catch (Exception e) {
log.error("Error inserting order", e);
return ResponseEntity.internalServerError().body(new GenericResponseDTO(e.getMessage()));
}
}
< /code>
und der folgende MockMVC-Test: < /p>
    @Test
@DisplayName("valid Content Type with no Body")
public void validContentTypeNoContent() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.post("/order")
.characterEncoding("UTF-8")
.contentType("application/raildex.OrderDTO.v1+json")
.accept("application/json"))
.andDo(print())
.andExpect(status().isBadRequest());
}
Der Test sollte einen Aufruf mit Inhaltstypanwendung/Raildex.OrderDto.v1+JSON ausführen und der Server sollte den Aufruf akzeptieren, aber die Validierung fehlschlagen (da kein angegebener Inhalt angegeben ist) und ein HTTP 400 zurückgeben. 415: < /p>

Code: Select all

MockHttpServletRequest:
HTTP Method = POST
Request URI = /order
Parameters = {}
Headers = [Content-Type:"application/raildex.OrderDTO.v1+json;charset=UTF-8", Accept:"application/json"]
Body =
Session Attrs = {}

[...]

MockHttpServletResponse:
Status = 415
Error message = Content-Type 'application/raildex.orderdto.v1+json;charset=UTF-8' is not supported.
Headers = [Accept:"application/json, application/yaml, application/xml;charset=UTF-8, text/xml;charset=UTF-8, application/*+json, application/*+xml;charset=UTF-8"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []

java.lang.AssertionError: Status expected: but was:
Expected :400
Actual   :415

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post