Hamcrest用"is"声明布尔字段.前缀获取方法 [英] Hamcrest assertion of boolean fields with an "is" prefix getter method

查看:222
本文介绍了Hamcrest用"is"声明布尔字段.前缀获取方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想断言一个自定义对象列表包含一个对象,该对象的某些字段具有某些值,并带有一系列这样的断言

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆