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
.
can you use this?
assertThat(map.get("key"), hasProperty("propertyName", equalTo("value")));
If you want to check that there is at least one key--any key--that matches the value, use hasValue
:
assertThat(map, hasValue(hasProperty("propertyName", is("value"))));
The difference is in the mismatch output. There are also matchers for hasKey
and hasEntry
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With