【问题标题】:Unable to run junit-5 test cases from intellij idea无法从 intellij idea 运行 junit-5 测试用例
【发布时间】:2018-02-15 18:08:41
【问题描述】:

Junit-5 测试用例从命令行成功为我运行。我正在使用 Maven,当我使用 mvn clean install 运行测试用例时运行成功,但是当使用 IntelliJ 运行单个测试用例时它失败并出现以下错误。

Feb 15, 2018 11:33:41 PM org.junit.jupiter.engine.discovery.JavaElementsResolver resolveMethod
WARNING: Method 'public static java.util.Collection com.softiventure.dtos.junit.parametrized.NestedParametrizedTest$sumTest.parameters()' could not be resolved.
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:59)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at 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)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

我的示例测试类是:

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@DisplayName("Pass paramters ")
public class ParametrizedJunit5Test {

    @DisplayName("Should pass a non-null message to our test method")
    @ParameterizedTest
    @ValueSource(strings = {"Hello", "World"})
    void shouldPassNonNullMessageAsMethodParameter(String message) {
        assertNotNull(message);
    }
}

任何帮助将不胜感激。

【问题讨论】:

标签: java unit-testing intellij-idea junit junit5


【解决方案1】:

JUnit5 和 IntelliJ 之间存在一些兼容性问题。

根据the JUnit docs...

建议使用 IDEA 2017.3 或更高版本,因为这些较新版本的 IDEA 会根据项目中使用的 API 版本自动下载以下 JAR:junit-platform-launcher、junit-jupiter-engine 和 junit-vintage -引擎。

来自 IntelliJ 团队的 blog post 也很重要。该帖子中有一个comment 来自与您遇到相同错误的用户,该用户的回复是:

我将idea更新到2017.2.6,效果很好。

所以,更新到 IntelliJ 2017.3 或更高版本,这个问题应该会消失。

【讨论】:

    【解决方案2】:

    根据官方评论,您可能需要升级您的 IDE 版本

    IDE 对旧的 junit 5 启动器 jar 有编译依赖,它与当前发布的版本不兼容。因此,您可以选择更新 IDE 使其与您使用的 junit 版本兼容,或者降级 junit 版本(检查 IDEA_INSTALLATION_HOME/plugins/junit/lib 中捆绑的版本)。 2017.1 仅对 junit 5 提供实验性支持,因为当时 junit 5 尚未发布。很抱歉给您带来不便。

    https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000791190-Intellij-does-not-run-Junit5-tests

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。我的IDEA版本是2017.1 ver.,在运行Junit5测试用例时,出现了blow错误。

      线程“主”java.lang.NoSuchMethodError 中的异常:org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V

      我将 IDEA 更新为 2017.3.5 ver.,问题已解决

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-01
        • 1970-01-01
        • 2011-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-26
        相关资源
        最近更新 更多