Getting "NoSuchMethodError: org.hamcrest.Matcher.describeMismatch" when running test in IntelliJ 10.5

Noel Yap picture Noel Yap · Oct 24, 2011 · Viewed 108.8k times · Source

I'm using JUnit-dep 4.10 and Hamcrest 1.3.RC2.

I've created a custom matcher that looks like the following:

public static class MyMatcher extends TypeSafeMatcher<String> {
    @Override
    protected boolean matchesSafely(String s) {
        /* implementation */
    }

    @Override
    public void describeTo(Description description) {
        /* implementation */
    }

    @Override
    protected void describeMismatchSafely(String item, Description mismatchDescription) {

        /* implementation */
    }
}

It works perfectly fine when run from the command line using Ant. But when run from IntelliJ, it fails with:

java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
    at com.netflix.build.MyTest.testmyStuff(MyTest.java:40)

My guess is that it's using the wrong hamcrest.MatcherAssert. How do I find which hamcrest.MatcherAssert it's using (ie which jar file it's using for hamcrest.MatcherAssert)? AFAICT, the only hamcrest jars in my classpath is 1.3.RC2.

Is IntelliJ IDEA using it's own copy of JUnit or Hamcrest?

How do I output the runtime CLASSPATH that IntelliJ is using?

Answer

Garrett Hall picture Garrett Hall · Oct 24, 2011

Make sure the hamcrest jar is higher on the import order than your JUnit jar.

JUnit comes with its own org.hamcrest.Matcher class that is probably being used instead.

You can also download and use the junit-dep-4.10.jar instead which is JUnit without the hamcrest classes.

mockito also has the hamcrest classes in it as well, so you may need to move\reorder it as well