Java Hamcrest Matchers.contains 匹配器不工作 (?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22720233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hamcrest Matchers.contains matcher not working (?)
提问by JulioAragao
I am trying to test if a collection has an item which toString() method returns a particular String. I tried do it using excellent Hamcrest matching classes, by combining contains with Matchers.hasToString
, but somehow, its Matchers.contains
is not being able to match an item even though it is present in the collection.
我正在尝试测试集合是否具有 toString() 方法返回特定字符串的项目。我尝试使用优秀的 Hamcrest 匹配类来做到这一点,通过将 contains 与 相结合Matchers.hasToString
,但不知何故,Matchers.contains
即使它存在于集合中,它也无法匹配项目。
Here's an example:
下面是一个例子:
class Item {
private String name;
public Item(String name){
this.name = name;
}
public String toString(){
return name;
}
}
// here's a sample collection, with the desired item added in the end
Collection<Item> items = new LinkedList<Item>(){{
add(new Item("a"));
add(new Item("b"));
add(new Item("c"));
}};
Assert.assertThat(items, Matchers.contains(Matchers.hasToString("c")));
The above assertion is not successful. Here's the message:
上面的断言是不成功的。这是消息:
java.lang.AssertionError:
Expected: iterable containing [with toString() "c"]
but: item 0: toString() was "a"
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:865)
at org.junit.Assert.assertThat(Assert.java:832)
It looks like the Matchers.contains matcher tries to iterate over the list, but the Matchers.hasToString matcher fails in the first item and invalidates the rest of the iteration. Hamcrest javadoc for Matchers.contains says:
看起来 Matchers.contains 匹配器试图迭代列表,但 Matchers.hasToString 匹配器在第一项中失败并使迭代的其余部分无效。Matchers.contains 的 Hamcrest javadoc 说:
"Creates a matcher for Iterables that matches when a single pass over the examined Iterable yields a single item that satisfies the specified matcher. For a positive match, the examined iterable must only yield one item"
“为 Iterable 创建一个匹配器,当单次通过检查的 Iterable 产生满足指定匹配器的单个项目时,该匹配器匹配。对于正匹配,检查的迭代器必须只产生一个项目”
Am I doing something wrong?
难道我做错了什么?
采纳答案by Sotirios Delimanolis
I think you're looking for Matchers.hasItem(..)
我想你正在寻找 Matchers.hasItem(..)
Assert.assertThat(items, Matchers.hasItem(Matchers.hasToString("c")));
which states
哪个国家
Creates a matcher for Iterables that only matches when a single pass over the examined
Iterable
yields at least one item that is matched by the specifieditemMatcher
. Whilst matching, the traversal of the examinedIterable
will stop as soon as a matching item is found.
为 Iterables 创建一个匹配器,该匹配器仅在单次通过检查
Iterable
产生至少一个与指定的 匹配的项目时匹配itemMatcher
。在匹配时,Iterable
一旦找到匹配项,将停止对检查对象的遍历。
Matchers.contains
, as you stated,
Matchers.contains
,正如你所说,
Creates a matcher for Iterables that matches when a single pass over the examined
Iterable
yields a single item that satisfies the specified matcher. For a positive match, the examined iterable must only yield one item.
为 Iterables 创建一个匹配器,当单次通过检查
Iterable
产生满足指定匹配器的单个项目时匹配。对于正匹配,检查的迭代必须只产生一个项目。
It seems to me like that's saying there should only be one element in the Iterable
.
在我看来,这似乎是说Iterable
.