在IntelliJ中但不在命令行上运行JUnit 5测试时出错

时间:2019-02-08 16:03:59

标签: java intellij-idea junit5

当我尝试在IntelliJ中运行简单的JUnit 5测试时,报告以下错误:

Feb 08, 2019 3:37:39 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable 
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests 
java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List; 
at org.junit.vintage.engine.discovery.JUnit4DiscoveryRequestResolver.filterAndConvertToTestClassRequests(JUnit4DiscoveryRequestResolver.java:79)
at org.junit.vintage.engine.discovery.JUnit4DiscoveryRequestResolver.resolve(JUnit4DiscoveryRequestResolver.java:48)
at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:64) 
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:177) 
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:164) 
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:120) 
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:49) 
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V 
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:61) 
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Process finished with exit code 1

这是我的pom:

<properties> 
    <java.version>1.8</java.version> 
    <junit.jupiter.version>5.3.2</junit.jupiter.version> 
</properties>

</dependencies> 
    <dependency> 
        <groupId>org.junit.jupiter</groupId> 
        <artifactId>junit-jupiter-api</artifactId> 
        <version>${junit.jupiter.version}</version> 
        <scope>test</scope> 
    </dependency> 
    <dependency> 
        <groupId>org.junit.jupiter</groupId> 
        <artifactId>junit-jupiter-params</artifactId> 
        <version>${junit.jupiter.version}</version> 
        <scope>test</scope> 
    </dependency>

    <!-- Only needed to run tests in a version of IntelliJ IDEA that bundles older versions --> 
    <dependency> 
        <groupId>org.junit.jupiter</groupId> 
        <artifactId>junit-jupiter-engine</artifactId> 
        <version>${junit.jupiter.version}</version> 
        <scope>test</scope> 
    </dependency> 
    <dependency> 
        <groupId>org.junit.platform</groupId> 
        <artifactId>junit-platform-launcher</artifactId> 
        <version>1.4.0</version> 
        <scope>test</scope> 
    </dependency> 
</dependencies>

<build> 
    <plugins> 
        <!-- JUnit 5 requires Surefire version 2.22.1 or higher --> 
        <plugin> 
            <artifactId>maven-surefire-plugin</artifactId> 
            <version>2.22.1</version> 
        </plugin> 
    </plugins> 
</build>

我正在使用Intellij v2017.1.2。

当我从命令行运行测试时,没有错误报告。

我已经看到很多帖子针对不同版本的IntelliJ报告了类似的问题,但是建议的解决方案没有帮助。

1 个答案:

答案 0 :(得分:2)

JUnit User Guide建议使用2017.3或更高版本。

  

IntelliJ IDEA早于IDEA 2017.3捆绑了特定版本的JUnit5。因此,如果要使用较新版本的JUnit Jupiter,则由于版本冲突,IDE中的测试执行可能会失败。