8

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.

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:

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

Am I doing something wrong?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
aragaojcna
  • 95
  • 1
  • 2
  • 4

1 Answers1

21

I think you're looking for 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 specified itemMatcher. Whilst matching, the traversal of the examined Iterable will stop as soon as a matching item is found.

Matchers.contains, as you stated,

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.

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

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Sotirios, I tried hasItem, but got a compiler error: The method assertThat(T, Matcher super T>) in the type Assert is not applicable for the arguments (Collection, Matcher>) – aragaojcna Mar 28 '14 at 18:45
  • @JulioAragao I copied your example code as is and replaced `contains` with `hasItem`. – Sotirios Delimanolis Mar 28 '14 at 18:52
  • Yes, I guess you're right @Sotirios. I changed the order in the list iterated by the contains matcher so that the "c" appeared first, but even so it doesn't pass. I will investigate why I am having this compilation problem, but I think you nailed it. Thanks! – aragaojcna Mar 28 '14 at 19:24
  • @julio you can edit your question with details and I'll try to help (but later as I'm away from a PC right now). – Sotirios Delimanolis Mar 28 '14 at 19:28
  • 1
    Thanks for the answer! But wow, what a poorly named method. Since when does "contains" mean "is composed of exactly this one thing"? – 0xbe5077ed Feb 23 '17 at 20:03