Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 795
fashuser Avatar asked May 26 '14 10:05

fashuser


2 Answers

can you use this?

assertThat(map.get("key"), hasProperty("propertyName", equalTo("value")));
like image 158
Simulant Avatar answered Sep 30 '22 19:09

Simulant


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.

like image 24
David Harkness Avatar answered Sep 30 '22 19:09

David Harkness