I have an test and when I do the expected in a Mock.perform, the list of "albuns" in the object "artista" are unordered and is the reason to broke my test. I just want to know if I can expect a list ordered by the id.
In the "Artista.class" I have a HashSet and I tried but failed to do the conversion or compareTo.
Please, sorry about my english and I expect that I was clear about the error.
@Test
public void salvarArtistaComAlbuns() throws Exception {
//instanciando albuns
Album album1 = new Album();
album1.setId(1L);
album1.setNome("Raimundos");
album1.setDescricao("Album de 2005");
Album album2 = new Album();
album2.setId(2L);
album2.setNome("Raimundos2");
album2.setDescricao("Album de 2008");
//Inserindo albuns em um set de albuns
Set<Album> albuns = new HashSet<>();
albuns.add(album1);
albuns.add(album2);
//instanciando artista
Artista artista = new Artista();
artista.setId(1L);
artista.setNome("Raimundos");
artista.setDescricao("Rock brasileiro");
artista.setAlbuns(albuns);
//Convertendo artista em uma string para JSON
String asJsonString = objectMapper.writeValueAsString(artista);
//Chamando metodo save e definindo retorno
when(artistaService.save(Mockito.any(Artista.class))).thenReturn(artista);
//Mock do metodo post
mockMvc.perform(
MockMvcRequestBuilders.post("/artistas").contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).accept(
MediaType.APPLICATION_JSON_UTF8_VALUE).content(asJsonString))
.andExpect(status().isCreated()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.id", is(1))).andExpect(jsonPath("$.nome", is("Raimundos"))).andExpect(
jsonPath("$.descricao", is("Rock brasileiro")))
.andExpect(jsonPath("$.albuns[1].id", is(1))).andExpect(
jsonPath("$.albuns[1].nome", is("Raimundos"))).andExpect(
jsonPath("$.albuns[1].descricao", is("Album de 2005")))
.andExpect(jsonPath("$.albuns[2].id", is(2))).andExpect(
jsonPath("$albuns[2].nome", is("Raimundos2"))).andExpect(
jsonPath("$.albuns[2].descricao", is("Album de 2008")));
//Verifica se o metodo SAVE foi chamado
verify(artistaService, times(1)).save(Mockito.any(Artista.class));
verifyNoMoreInteractions(artistaService);
assertNotNull(artista.getId());
assertThat(artista.getNome(), is("Raimundos"));
assertThat(artista.getDescricao(), is("Rock brasileiro"));
assertNotNull(album1.getId());
assertThat(album1.getNome(), is("Raimundos"));
assertThat(album1.getDescricao(), is("Album de 2005"));
assertNotNull(album2.getId());
assertThat(album2.getNome(), is("Raimundos2"));
assertThat(album2.getDescricao(), is("Album de 2008"));
assertEquals(albuns.size(), 2);
}