在進行單元測試時,測試出現異常
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
錯誤就在pom.xml
的依賴中,仔細查看控制台輸出你會發現IntelliJ IDEA正在嘗試使用JUnit5運行我的測試用例。
at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
- 1
通過pom.xml
發現,我希望使用JUnit4.12運行測試用例,我們查看pom.xml
發現junit-jupiter-api這個依賴會導致這個錯誤。
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> </dependency>
- 1
- 2
- 3
- 4
- 5
因為此模塊專為JUnit5而設計 -https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
解決方案
1、刪除
pom.xml
中org.junit.jupiter
依賴
2、Reimport All Maven Project