I'm trying to test a POST and no matter what I do I get the java.lang.AssertionError: Content type not set
My controller:
@RestController
@RequestMapping("/artistas")
public class ArtistaEndpoint {
private final ArtistaService artistaService;
private final ArtistaMapper artistaMapper;
private final AlbumService albumService;
@CrossOrigin(origins = "http://localhost:8080")
@PostMapping
public ResponseEntity<Void> post(@Valid @RequestBody Artista artista) {
artista = artistaService.save(artista);
ArtistaDto artistaDto = artistaMapper.toDtoCompleto(artista);
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(artistaDto.getId()).toUri();
if(!artista.getAlbuns().isEmpty()) {
Set<Album> albuns = artista.getAlbuns();
for(Album album: albuns) {
album.setArtista(artista);
albumService.save(album);
}
}
return ResponseEntity.created(uri).build();
}
}
and my test:
@Test
public void salvarArtistaSemAlbum() throws Exception {
Artista adicionado = new Artista();
adicionado.setId(1L);
adicionado.setNome("Banda Eva");
adicionado.setDescricao("Viva o carnaval");
when(artistaService.save(Mockito.any(Artista.class))).thenReturn(adicionado);
mockMvc.perform(MockMvcRequestBuilders.post("/artistas")
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)and
.content(TestUtil.asJsonString(adicionado)))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.id", is(1)))
.andExpect(jsonPath("$.nome", is("Banda Eva")))
.andExpect(jsonPath("$.descricao", is("Viva o carnaval")));
verify(artistaService, times(1)).save(Mockito.any(Artista.class));
verifyNoMoreInteractions(artistaService);
assertNull(adicionado.getId());
assertThat(adicionado.getNome(), is("Banda Eva"));
assertThat(adicionado.getDescricao(), is("Viva o carnaval"));
}
And the response of the httpservlet:
MockHttpServletResponse:
Status = 201
Error message = null
Headers = {Location=[http://localhost/artistas/1]}
Content type = null
Body =
Forwarded URL = null
Redirected URL = http://localhost/artistas/1
Cookies = []
I already did a question but the error was another, and I could resolve. I don't understand why it's returning 201 created and no content body. Smells like some annotation that I haven't, but I already review with no sucess. Srry about my english and thanks for the help.
EDIT-
I thought the problem was solved but I was wrong