1

I am currently testing hasItem() Matcher but to no avail. Please see sample code below:

List<String> list = new ArrayList<String>();

list.add("1");
list.add("2");
list.add("3");

org.junit.Assert.assertThat(list, hasItem("3"));

It produces

java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
    at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:31)
    at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:14)
    at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:12)
    at org.junit.Assert.assertThat(Assert.java:865)
    at org.junit.Assert.assertThat(Assert.java:832)
    ...
Rey Libutan
  • 4,907
  • 7
  • 34
  • 67
  • 1
    [This](http://stackoverflow.com/questions/7869711/getting-nosuchmethoderror-org-hamcrest-matcher-describemismatch-when-running) might help. – devnull Mar 24 '14 at 09:29
  • 1
    This usually happens when you have imported different versions of Hamcrest-all, Hamcrest-core or it is in conflict with the Hamcrest that comes with JUnit. Try using JUnit-dep (the one without dependencies). – John B Mar 24 '14 at 10:23

1 Answers1

1

I did a local test and it works pretty fine for me using JUnit-4.11 and Hamcrest-Core-1.3:

import java.util.ArrayList;
import java.util.List;

import org.hamcrest.core.IsCollectionContaining;
import org.junit.Test;

public class Example {
    @Test public void test() {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        org.junit.Assert.assertThat(list, IsCollectionContaining.hasItem("3"));
    }
}

Could you try it again using same library versions and imports as I did?

Harmlezz
  • 7,524
  • 23
  • 34