使用Junit 5运行测试时的java.lang.NoSuchMethodError
我创建了一个简单的测试来尝试Junit 5:
import org.junit.jupiter.api.Test; public class MyTest { @Test public void testJupiter() { System.out.println("test"); } }
这是我使用的依赖:
org.junit.jupiter junit-jupiter-api 5.0.0 test
堆栈跟踪是下一个:
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader; at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:30) at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:53) at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:49) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
出了什么问题?
我看到你使用IntelliJ,它只支持特定版本的jUnit5。
Junit5 – 在IntelliJ中运行测试
试试例如
5.0.0-M4
除了依赖,你还在pom.xml
配置了JUnit5插件吗? 它看起来应该是这样的(取自文档 ):
... ... maven-surefire-plugin 2.19 org.junit.platform junit-platform-surefire-provider 1.0.0 org.junit.jupiter junit-jupiter-engine 5.0.0 ... ... org.junit.jupiter junit-jupiter-api 5.0.0 test ...