[英]How to use Hamcrest's AssertThat for String[]
So I've been looking around and trying to find a solution to this problem but I'm coming up with either compiler errors or weird expectations or both. 所以我一直在四处寻找解决这个问题的方法,但是我遇到了编译器错误或怪异的期望,或两者兼而有之。 So here we go: 所以我们开始:
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"));
So this code compile fine but when I go to build it in Maven the test fails because: Expected: hasProperty("John"), got:[John]
因此,此代码可以正常编译,但是当我在Maven中进行构建时,测试失败,原因是: Expected: hasProperty("John"), got:[John]
So I did some looking and checked out the other questions people got answered here but I get compile errors, I am clearly doing the assertThat wrong but how should the assertThat be set up? 所以我做了一些检查,并检查了其他人在这里回答的问题,但是我遇到了编译错误,我显然在做assertThat错误,但是应该如何设置assertThat?
Use the hasItemInArray
matcher: 使用hasItemInArray
匹配器:
assertThat(fname, hasItemInArray("John"));
The hasProperty
matcher matches Java Bean properties. hasProperty
匹配器匹配Java Bean属性。
If you want to assert that array fname
contains the item John
and nothing else you can use the IsArrayContainingInOrder matcher ( Matchers.arrayContaining
): 如果要断言阵列fname
包含该项目John
并没有别的你可以使用IsArrayContainingInOrder匹配( Matchers.arrayContaining
):
assertThat(fname, arrayContaining("John"));
If you only care that at least one item in fname
is John
use the IsArrayContaining matcher ( Matchers.hasItemInArray
) as suggested by @hzpz. 如果你只关心在至少一个项目fname
是John
使用IsArrayContaining匹配( Matchers.hasItemInArray
由@hzpz建议)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.