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 ...
IntelliJ团队的blog post也与此相关。遇到与您相同的错误的用户在该帖子上有一个comment,该用户的响应是:
因此,更新至IntelliJ 2017.3或更高版本,此问题应消失。
关于java - 无法从IntelliJ IDEA运行JUnit-5测试用例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48813715/