Hamcrest Matchers.包含匹配器不起作用(?) [英] Hamcrest Matchers.contains matcher not working (?)

查看:167
本文介绍了Hamcrest Matchers.包含匹配器不起作用(?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试集合中是否包含toString()方法返回特定String的项目.我尝试使用出色的Hamcrest匹配类,通过将contains与Matchers.hasToString结合使用,但是以某种方式,即使它在集合中,它的Matchers.contains也无法匹配该项目.

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.

这是一个例子:

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)

看起来Matchers.contains匹配器试图遍历列表,但是Matchers.hasToString匹配器在第一项中失败,并使其余的迭代无效. Hamcrest javadoc for Matchers.contains说:

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:

为Iterables创建一个匹配项,当在经过检查的Iterable上进行一次遍历时,将产生一个满足指定匹配项的项目.对于正匹配,被检查的Iterable只能产生一项."

"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"

我做错什么了吗?

推荐答案

我认为您正在寻找Matchers.hasItem(..)

Assert.assertThat(items, Matchers.hasItem(Matchers.hasToString("c")));

其中陈述

为Iterables创建一个匹配器,该匹配器仅在单次通过时才匹配 超过检查的Iterable会产生至少一项与 指定的itemMatcher.匹配时,遍历 一旦找到匹配项,被检查的Iterable将立即停止.

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 specified itemMatcher. Whilst matching, the traversal of the examined Iterable will stop as soon as a matching item is found.

如您所说

Matchers.contains

为Iterables创建一个匹配器,该匹配器在单次传递时进行匹配 被检查的Iterable产生满足以下条件的单个项目 指定的匹配器.对于正匹配,所检查的可迭代项必须 只能产生一项.

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中应该只有一个元素.

It seems to me like that's saying there should only be one element in the Iterable.

这篇关于Hamcrest Matchers.包含匹配器不起作用(?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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