新建了一个springboot项目,内含junit模块,打算试一下junit的功能,谁知启动就报错,具体错误如下:
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:31) at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:42) at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:43) 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)百度一查,我的junit版本是junit5,junit5在低版本的IDEA上会报这个错误,本人使用的idea版本确实比较老(2016.3.4),有人说需要升级idea版本即可,我没有尝试,因为是自己练习的,觉得安装新版本比较麻烦,所以尝试了降低junit版本即可: 将pom中spring-boot-starter-parent的版本降低由2.3.3.RELEASE降为2.1.5.RELEASE(仅此一步搞定)
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>按照junit4的语法进行编写test即可,再次运行,成功