Hamcrest assertion of boolean fields with an “is” prefix getter method

雨燕双飞 提交于 2019-12-10 14:53:40

问题


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?


回答1:


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)




回答2:


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



来源:https://stackoverflow.com/questions/32987750/hamcrest-assertion-of-boolean-fields-with-an-is-prefix-getter-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!