IntelliJ 10.5 でテストを実行すると「NoSuchMethodError: org.hamcrest.Matcher.describeMismatch」が発生する 質問する

IntelliJ 10.5 でテストを実行すると「NoSuchMethodError: org.hamcrest.Matcher.describeMismatch」が発生する 質問する

私は JUnit-dep 4.10 と Hamcrest 1.3.RC2 を使用しています。

次のようなカスタム マッチャーを作成しました。

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 */
    }
}

Ant を使用してコマンドラインから実行すると、問題なく動作します。しかし、IntelliJ から実行すると、次のエラーで失敗します。

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)

私の推測では、間違った hamcrest.MatcherAssert を使用していると思います。どの hamcrest.MatcherAssert を使用しているか (つまり、hamcrest.MatcherAssert にどの jar ファイルを使用しているか) をどうやって見つければよいですか? 私のクラスパスにある hamcrest jar は 1.3.RC2 だけです。

IntelliJ IDEA は独自の JUnit または Hamcrest のコピーを使用していますか?

IntelliJ が使用しているランタイム CLASSPATH を出力するにはどうすればよいですか?

ベストアンサー1

インポート順序で、hamcrest jar がJUnit jar よりも上位にあることを確認します。

JUnitには独自のorg.hamcrest.Matcherクラスが付属しており、代わりにそれが使用される可能性があります。

代わりに、hamcrest クラスのない JUnit であるjunit-dep-4.10.jar をダウンロードして使用することもできます。

mockitoにはhamcrestクラスも含まれているので、move\reorderも必要になるかもしれません。

おすすめ記事