Assert key in hashmap JUnit

junit

I thought it was going to be easy but can anyone tell me how I can test HashMap to see if it has some value in JUnit?

assertThat(myMap, ??);

I tried something like:

assertThat(myMap, hasEntry("Key", notNullValue()));

…But I couldn't make it compile since my import to hasEntry and notNullValue() is correct.
Does anyone know what the correct import pkg for them should be?

Best Answer

You're after hasEntry(Matcher<? super K> keyMatcher, Matcher<? super V> valueMatcher).

The underlying implementation is in IsMapContaining but, like most matchers in Hamcrest, it can also be found through org.hamcrest.Matchers, in hamcrest-library.

Otherwise, your syntax is correct, and Matchers also defines notNullValue().