Hamcrest用"is"声明布尔字段.前缀获取方法 [英] Hamcrest assertion of boolean fields with an "is" prefix getter method
问题描述
我们想断言一个自定义对象列表包含一个对象,该对象的某些字段具有某些值,并带有一系列这样的断言
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))));
不过,自定义对象还具有布尔类型字段,其中getter方法的前缀为"is"而不是"get",并且断言似乎因以下原因而失败
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?
推荐答案
Hamcrest在内部使用实现标准JavaBean
行为的java.beans.PropertyDescriptor
,仅将is
用于布尔型基本类型.
Hamcrest uses internally the java.beans.PropertyDescriptor
that implements the standard JavaBean
behaviour, allowing the is
just for the boolean primitive types.
恐怕您将不得不创建自己的Matcher
(类似于hasGetterValue)
I'm afraid you will have to create your own Matcher
(something like hasGetterValue)
这篇关于Hamcrest用"is"声明布尔字段.前缀获取方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!