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

标签 java unit-testing intellij-idea junit junit5

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

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

最佳答案

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来自 IntelliJ 团队也很重要。有一个 comment在遇到与您相同错误的用户的帖子中,该用户的回复是:

I update idea to 2017.2.6, it works well.

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

关于java - 无法从 intellij idea 运行 junit-5 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48813715/

相关文章:

java - SWT:无法在 Mac 上调整按钮大小

java - Squarespace-Wells 模板 - 页脚/ Logo /悬停

javascript - 为什么这个 QUnit RegExp 测试失败了?

c# - 单元测试中的元素在完成后仍待处理

java - 在 IDE 调试器中隐藏堆栈帧(例如 Java9 StackWalker)

java - 我编写的代码有问题吗?

java - 为什么这里被零除不触发异常?

unit-testing - 如何对 webrtc 应用进行单元测试?

java - Intellij 中出现错误 "Package org.eclipse.... does not exist"

android-studio - Windows 10 : Android Studio Start Failed: Internal error. 没有报告修复工作