Search code examples
javaunit-testingjunitasserthamcrest

Hamcrest equal collections


Is there a matcher in Hamcrest to compare collections for equality? There is contains and containsInAnyOrder but I need equals not bound to concrete collection type. E.g. I cannot compare Arrays.asList and Map.values with Hamcrest equals.

Thanks in advance!


Solution

  • I cannot compare Arrays.asList and Map.values with Hamcrest equals.

    This is because of hamcrest's over-zealous type signatures. You can do this equality comparison, but you need to cast the List object to Collection before it'll compile.

    I often have to do casting with Hamcrest, which feels wrong, but it's the only way to get it to compile sometimes.