Hamcrest test shows that a card contains all records from another card

I want to check that the card contains a specific set of records. It may contain other entries that are not in expectedMap. I currently have the following statement:

assertThat(expectedMap.entrySet(), everyItem(isIn(actualMap.entrySet())));

Although this works, the error message it prints is confusing because the expected and received arguments were canceled from normal use. Is there a better way to write this?

+4
source share
1 answer

hasItems , , . , Hamcrest Iterable s. ( , , Java-, ).

( , generics, Map<String, String>, Map).

...

, / @SuppressWarnings("unchecked") :

assertThat(actualMap.entrySet(), (Matcher)hasItems(expectedMap.entrySet().toArray()));

: hasItems, Set Iterable, . Set.toArray() Object[], assertThat actualMap.entrySet(), , .

, , - Set - Iterable<Object> ( ), :

assertThat(new HashSet<Object>(actualMap.entrySet()), hasItems(expectedMap.entrySet().toArray()));

, , , , :

for (Entry<String, String> entry : expectedMap.entrySet()) {
    assertThat(actualMap, hasEntry(entry.getKey(), entry.getValue())); 
}

... Matcher - , SO.

+5

All Articles