ham*_*est 5 java junit hamcrest
我们想声明一个自定义对象列表包含一个对象,其中某些字段具有某些值,并带有一系列断言,如下所示
assertThat(customObjectList, hasItem(hasProperty("someField", equalTo(someValue))));
Run Code Online (Sandbox Code Playgroud)
但是自定义对象也有布尔类型字段,其中getter方法有一个"is"前缀而不是"get",并且断言似乎失败了
java.lang.AssertionError: Expected: a collection containing hasProperty("booleanField", <true>) but: property "booleanField" is not readable
Run Code Online (Sandbox Code Playgroud)
是否有一个开箱即用的解决方案来解决这个问题,或者它应该用某种自定义匹配器来处理?
(将评论转移到答案,正如Ivo Mori所建议的那样)
Hamcrest在内部使用java.beans.PropertyDescriptor它来实现标准JavaBean行为,允许is仅用于布尔基元类型.
我担心你必须创建自己的Matcher(像hasGetterValue)
| 归档时间: |
|
| 查看次数: |
3035 次 |
| 最近记录: |