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!
Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluable, such as UI validation or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used.
Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.
Hamcrest vs TestNG TestNG is a testing framework, and as noted above, Hamcrest is a matcher framework. Both frameworks allow us to make assertions; it is here where Hamcrest does a better job than TestNG. TestNG is a testing framework, and as noted above, Hamcrest is a matcher framework.
Purpose of the Hamcrest matcher framework. Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With