获取:在IntelliJ中运行JUnit测试,获取:java.lang.NoSuchMethodError:org.junit.platform.commons.util.Preconditions.notNull

时间:2018-04-03 16:25:18

标签: intellij-idea junit

我试图在IntelliJ中运行单元测试,并且我正在获取堆栈跟踪:

线程中的异常" main" java.lang.NoSuchMethodError:org.junit.platform.commons.util.Preconditions.notNull([Ljava / lang / Object; Ljava / lang / String;)[Ljava / lang / Object;     在org.junit.platform.launcher.core.DefaultLauncher.registerTestExecutionListeners(DefaultLauncher.java:71)     at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:44)     at com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)     在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)     在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:498)     在com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Google搜索错误消息并没有帮助 - 我收到的所有点击都与Gradle依赖关系有关,但我使用的是Maven(我已经对我的依赖关系进行了三重检查)。这是我的pom:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.0.0-M3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.0.0-M3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.0.0-M3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

令人沮丧的是,这确实在另一个项目中有效,所以我不确定这里出了什么问题。

1 个答案:

答案 0 :(得分:0)

没关系,我想出来 - 如果其他人有同样的问题:原来我的单元测试是导入org.junit.Test,而不是org.junit.jupiter.api.Test,但我没有在我的pom.xml中声明对junit-vintage的依赖。解决方案是在我的pom中明确声明junit-vintage的正确版本(在我的情况下为4.12.0-M3),或者将我的单元测试切换到import org.junit.jupiter.api.Test。