1、背景:
Junit測試出現異常:Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.
2、現象:
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)
Process finished with exit code 1
Empty test suite.
3、原因:
不能同時用Junit4和Junit5,junit-jupiter-api在Junit5中使用
4、解決辦法:
1)、排除掉 junit-jupiter-api 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
</exclusions>
</dependency>
2)、官方建議如果跑Junit5建議使用IDEA 2017.3之後的版本.因爲較之前的版本還不支持Junit5, 查看自己本地的IDEA爲2017.1的版本, 後升級2017.3.5版本後, 在執行Junit5後問題解決.
參考資料: Junit5官方guide https://junit.org/junit5/docs/current/user-guide/#running-tests-ide