猿问

在IntelliJ 10.5中运行测试时得到“ NoSuchMethodError:

我正在使用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文件)?AFAICT,在我的类路径中唯一的hamcrest jar是1.3.RC2。


IntelliJ IDEA是否使用它自己的JUnit或Hamcrest副本?


如何输出IntelliJ使用的运行时CLASSPATH?


慕码人8056858
浏览 589回答 3
3回答

守候你守候我

当您已经在类路径上使用了mockito-all时,也会出现此问题。如果可能的话,只需包括模仿主体。用于混合junit,mockito和hamcrest的Maven配置:<dependencies>&nbsp; <dependency>&nbsp; &nbsp; <groupId>org.hamcrest</groupId>&nbsp; &nbsp; <artifactId>hamcrest-core</artifactId>&nbsp; &nbsp; <version>1.3</version>&nbsp; &nbsp; <scope>test</scope>&nbsp; </dependency>&nbsp; <dependency>&nbsp; &nbsp; <groupId>org.hamcrest</groupId>&nbsp; &nbsp; <artifactId>hamcrest-library</artifactId>&nbsp; &nbsp; <version>1.3</version>&nbsp; &nbsp; <scope>test</scope>&nbsp; </dependency>&nbsp; <dependency>&nbsp; &nbsp; <groupId>org.mockito</groupId>&nbsp; &nbsp; <artifactId>mockito-all</artifactId>&nbsp; &nbsp; <version>1.9.5</version>&nbsp; &nbsp; <scope>test</scope>&nbsp; </dependency>&nbsp; <dependency>&nbsp; &nbsp; <groupId>junit</groupId>&nbsp; &nbsp; <artifactId>junit</artifactId>&nbsp; &nbsp; <version>4.11</version>&nbsp; &nbsp; <scope>test</scope>&nbsp; </dependency></dependencies>

哆啦的时光机

问题是使用了错误的类而hamcrest.Matcher不是hamcrest.MatcherAssert类。这是从junit-4.8依赖项中提取的,这是我的一个依赖项。要查看在测试时从哪个来源包含了哪些依赖项(和版本),请运行:mvn dependency:tree -Dscope=test
随时随地看视频慕课网APP

相关分类

Java
我要回答