How to compare two List<Float> in Junit?

Asked

Viewed 242 times

0

I need to compare two Lists,

assertEquals(lista1, lista2);
Assert.assertTrue(lista1.containsAll(lista2));

And the error the test shows is

(index:22 size:22) java.lang.Indexoutofboundsexception

Until today I had never tested a return List so I don’t know if I’m doing it right.

1 answer

0


Use the methods assertThat class MatcherAssert and the method is class CoreMatchers. Example:

@Test
public void verificaSeAListaASerTestadaEIgualAEsperada() {
    List<Float> listaASerTestada = Arrays.asList(1.0f, 2.0f, 3.0f);
    List<Float> listaEsperada = Arrays.asList(1.0f, 2.0f, 3.0f);
    MatcherAssert.assertThat(listaASerTestada, CoreMatchers.is(listaEsperada));
}
  • Thanks for the help I tested, keep giving error but I’ll see if I find another solution.

  • @Thiago16oli You can put the lists you are trying to test?

  • The problem is in the method that returned the list I tested with lists I created within the test and passed,so I will rewrite otherwise my code found a little better way to do it but thank you so much for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.