8

We would like to assert that a list of custom objects contains an object with some of its fields having certain values, with a series of assertions like this

assertThat(customObjectList, hasItem(hasProperty("someField", equalTo(someValue))));

However the custom object has also boolean type fields, where the getter method has an "is" prefix instead of "get", and there the assertion seems to fail with

java.lang.AssertionError: Expected: a collection containing hasProperty("booleanField", <true>) but: property "booleanField" is not readable

Is there an out-of-the-box solution to overcome this, or it should be handled with some kind of custom matcher?

hammerfest
  • 2,203
  • 1
  • 20
  • 43
  • This should work with primitive `boolean`s. Are you maybe talking about `Boolean` Objects? – Ruben Oct 07 '15 at 10:17
  • 1
    Thank you. Indeed, the fields are Boolean type. The class in question is actually JPA/Hibernate entity with all of its field types being objects and not primitives. We can not modify the class itself and have to find some solution to circumvent it in the tests – hammerfest Oct 07 '15 at 10:54

2 Answers2

7

Hamcrest uses internally the java.beans.PropertyDescriptor that implements the standard JavaBean behaviour, allowing the is just for the boolean primitive types.

I'm afraid you will have to create your own Matcher (something like hasGetterValue)

Chei
  • 2,117
  • 3
  • 20
  • 33
Ruben
  • 3,986
  • 1
  • 21
  • 34
1

FYI: one can use Hamcrest extension shazamcrest and it's sameBeanAs (DiagnosingCustomisableMatcher) which works perfectly fine even for Boolean types ;)

chipiik
  • 1,970
  • 15
  • 15