0

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);

}
Raagatzo
  • 27
  • 1
  • 6
  • 1
    In Java `HashSet`s are not ordered, they neither will guarantee that the order of the elements will remain constant over time. You should probably use `TreeSet` if you want to maintain the order of the elements. – Ervin Szilagyi Aug 06 '18 at 19:14
  • Do you want it by the name or the id? My answer is looking at the name, but you can change that if need be – Dan Aug 06 '18 at 19:22
  • Possible duplicate of [How to sort an ArrayList?](https://stackoverflow.com/questions/16252269/how-to-sort-an-arraylist) – Dan Aug 06 '18 at 19:38
  • Ervin, thanks for the answer... I tried you solution but I cant make done... I tried using a TreeSet and implemented the compareTo in my Album.class... – Raagatzo Aug 06 '18 at 20:00

1 Answers1

0

Try sorting (May have to convert to List)

albuns.sort((a1, a2) -> a1.getNome().compareTo(a2.getNome());
Dan
  • 979
  • 1
  • 8
  • 29
  • I think you need to begin Googling some of the recommendations you get here. https://www.mkyong.com/java/how-to-convert-list-to-set-arraylist-to-hastset/ – Dan Aug 06 '18 at 19:55
  • maybe I done something wrong, but I tried the convertion that you send and thats fail... – Raagatzo Aug 06 '18 at 20:10