Search code examples
javaunit-testingjunithamcrest

collection equality using hamcrest


I want to assert equality of two collection using hamcrest.

I know there is contains matcher but I want it to ignore ordering.

{1,2} is equals to {2,1} for my need.

What is the right syntax?


Solution

  • Use containsInAnyOrder() :

      List colors = Arrays.asList("red","green","blue");
      assertThat(colors, containsInAnyOrder("green", "red", "blue")); // is true