hasItems
does what you want, but in a non-obvious way. You have to jump through some hoops as Hamcrest has relatively limited support for matching Iterable
s. (Without going into detail, this is due to the vagaries of how Java generics work- I'll post some more links with detail later).
(I'm assuming you're using generics, e.g. Map<String, String>
as opposed to simply Map
).
In the meantime you have a couple of options...
If you are happy with test code that raises warnings / using @SuppressWarnings("unchecked")
in your test:
assertThat(actualMap.entrySet(), (Matcher)hasItems(expectedMap.entrySet().toArray()));
Explanation: there is no overload of hasItems
that takes a Set
or Iterable
, but it will take an array. Set.toArray()
returns Object[]
, which won't match assertThat
against your actualMap.entrySet()
- but if you erase the declared type of the Matcher, it will happily proceed.
If you want an assertion that compiles without warnings, it gets uglier - you need to copy the Set
into some kind of Iterable<Object>
(you can't cast) in order to match on the Objects :
assertThat(new HashSet<Object>(actualMap.entrySet()), hasItems(expectedMap.entrySet().toArray()));
But to be perfectly honest, for clarity you are almost certainly best off asserting each entry individually:
for (Entry<String, String> entry : expectedMap.entrySet()) {
assertThat(actualMap, hasEntry(entry.getKey(), entry.getValue()));
}
...or you could write your own Matcher - there are plenty of resources on how to do this online and on SO.