Skip to content
Advertisement

Map equality using Hamcrest

I’d like to use hamcrest to assert that two maps are equal, i.e. they have the same set of keys pointing to the same values.

My current best guess is:

JavaScript

which gives:

The method assertThat(T, Matcher<T>) in the type Assert is not applicable for the arguments (Set<Map.Entry<Householdtypes,Double>>, Matcher<Iterable<Set<Map.Entry<Householdtypes,Double>>>>)

I’ve also looked into variations of containsAll, and some others provided by the hamcrest packages. Can anyone point me in the right direction? Or do I have to write a custom matcher?

Advertisement

Answer

The shortest way I’ve come up with is two statements:

JavaScript

But you can probably also do:

JavaScript

depending on the implementations of the maps, and sacrificing the clarity of the difference report: that would just tell you that there is a difference, while the statement above would also tell you which one.

UPDATE: actually there is one statement that works independently of the collection types:

JavaScript
Advertisement