devo semplice test di integrazioneCome controllare String nel corpo di risposta con mockMvc
@Test
public void shouldReturnErrorMessageToAdminWhenCreatingUserWithUsedUserName() throws Exception {
mockMvc.perform(post("/api/users").header("Authorization", base64ForTestUser).contentType(MediaType.APPLICATION_JSON)
.content("{\"userName\":\"testUserDetails\",\"firstName\":\"xxx\",\"lastName\":\"xxx\",\"password\":\"xxx\"}"))
.andDo(print())
.andExpect(status().isBadRequest())
.andExpect(?);
}
In ultima riga voglio mettere a confronto stringa ricevuta nel corpo risposta alla stringa prevista
E in risposta ottengo:
MockHttpServletResponse:
Status = 400
Error message = null
Headers = {Content-Type=[application/json]}
Content type = application/json
Body = "Username already taken"
Forwarded URL = null
Redirected URL = null
Provato alcuni trucchi con content(), body() ma niente ha funzionato.
Proprio come consiglio, 400 codice di stato non deve essere restituito per qualcosa come '" Nome utente già preso "'. Questo dovrebbe essere più di un 409 conflitto. –
Grazie - l'obiettivo di questo test è specificare tali cose. – abrasadera