Search code examples
junit

Assert key in hashmap 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?


Solution

  • import static org.junit.Assert.AssertTrue;
    
    assertTrue(myMap.containsKey("yourKey") && myMap.get("yourKey") != null)