无法从 intellij idea 运行 junit-5 测试用例

Unable to run junit-5 test cases from intellij idea

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)

我的样本测试 class 是:

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);
    }
}

如有任何帮助,我们将不胜感激。

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

根据the JUnit docs ...

it is recommended to use IDEA 2017.3 or newer since these newer versions of IDEA will download the following JARs automatically based on the API version used in the project: junit-platform-launcher, junit-jupiter-engine, and junit-vintage-engine.

这个 blog post from the IntelliJ team is also relevant. There's a comment 关于那个 post 的用户遇到了与您相同的错误,该用户的回复是:

I update idea to 2017.2.6, it works well.

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

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

IDE has compilation dependency on the old junit 5 launcher jar and it is not compatible with current released version. So you have a choice to update IDE so it will be compatible with the junit version you use or to downgrade the junit version (check what version was bundled in IDEA_INSTALLATION_HOME/plugins/junit/lib). 2017.1 had only experimental support for junit 5 as junit 5 was not released yet at that time. Sorry for the inconvenience.

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

我遇到了同样的问题。我的IDEA版本是2017.1 ver.,当我运行 Junit 5测试用例时,出现了下面的错误。

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V

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