在進行單元測試時,測試出現異常java
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)
https://junit.org/junit5/docs/current/user-guide/#running-tests-ide-intellij-ideaapi
pom.xml
依賴以下intellij-idea
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> </dependency> </dependencies>
問題緣由
錯誤就在pom.xml
的依賴中,仔細查看控制檯輸出你會發現IntelliJ IDEA正在嘗試使用JUnit5運行個人測試用例。ide
at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
經過pom.xml
發現,我但願使用JUnit4.12運行測試用例,咱們查看pom.xml
發現junit-jupiter-api這個依賴會致使這個錯誤。單元測試
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> </dependency>
由於此模塊專爲JUnit5而設計 -https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api測試
解決方案ui
一、刪除
pom.xml
中org.junit.jupiter
依賴
二、Reimport All Maven Projectidea