如何使用Hamcrest的AssertThat for String []

时间:2015-07-15 20:19:57

标签: java maven hamcrest assertthat

所以我一直在环顾四周,试图找到解决这个问题的方法,但我想出的是编译器错误或奇怪的期望,或两者兼而有之。 所以我们走了:

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]

所以我做了一些查看并检查了人们在这里得到的其他问题,但是我得到了编译错误,我显然做了断言错误但是如何设置断言?

2 个答案:

答案 0 :(得分:2)

使用inflater.inflate(layoutId, parent, false); 匹配器:

hasItemInArray

assertThat(fname, hasItemInArray("John")); 匹配器匹配Java Bean属性。

答案 1 :(得分:1)

如果您想断言数组fname包含项John而没有其他任何内容,您可以使用IsArrayContainingInOrder匹配器(Matchers.arrayContaining):

assertThat(fname, arrayContaining("John"));

如果您只关心fname中至少有一项John使用@ {hzpz建议的IsArrayContaining匹配器(Matchers.hasItemInArray)。