Search code examples
javajunithamcrest

Verify map with custom values


I have the following map:

Map<String, MyCustomObject>

My goal is to verify capacity of this map using hamcrest matchers. I've tried the following approach:

assertThat(map, hasEntry("key", (MyCustomObject)hasItem(hasProperty("propertyName", equalTo("value")))));

But looks like that hasItem method works only with collections.

Is there any alternative method for verifying custom objects?

new MyCustomObject() does not work in my case because test fails by hashcode equality. And, another thing is that I cannot modify MyCustomObject class.


Solution

  • can you use this?

    assertThat(map.get("key"), hasProperty("propertyName", equalTo("value")));