![](/img/trans.png)
[英]How to use hamcrest nullValue with type inference in assertThat
[英]How to use Hamcrest's AssertThat for String[]
所以我一直在四处寻找解决这个问题的方法,但是我遇到了编译器错误或怪异的期望,或两者兼而有之。 所以我们开始:
this.mockPersonNode.setProperty("fname",new String[] {"John"});
...unrelated code...
//validate traits
final String[] fname = (String[]) groovy.getProperty("firstName");
//This is where my problems lie
assertThat(fname, hasProperty("John"));
因此,此代码可以正常编译,但是当我在Maven中进行构建时,测试失败,原因是: Expected: hasProperty("John"), got:[John]
所以我做了一些检查,并检查了其他人在这里回答的问题,但是我遇到了编译错误,我显然在做assertThat错误,但是应该如何设置assertThat?
使用hasItemInArray
匹配器:
assertThat(fname, hasItemInArray("John"));
hasProperty
匹配器匹配Java Bean属性。
如果要断言阵列fname
包含该项目John
并没有别的你可以使用IsArrayContainingInOrder匹配( Matchers.arrayContaining
):
assertThat(fname, arrayContaining("John"));
如果你只关心在至少一个项目fname
是John
使用IsArrayContaining匹配( Matchers.hasItemInArray
由@hzpz建议)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.