新建了一个2.2.6.RELEASE版本的springboot项目,在用单元测试的时候,发现没有@RunWith注解,于是,就添加了
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
好,现在有这个注解了,运行测试方法,报错java.lang.NoClassDefFoundError: org/junit/platform/engine/support/filter/ExclusionReasonConsumingFilter
警告: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoClassDefFoundError: org/junit/platform/engine/support/filter/ExclusionReasonConsumingFilter
at org.junit.vintage.engine.discovery.VintageDiscoverer.createTestClassPredicate(VintageDiscoverer.java:83)
at org.junit.vintage.engine.discovery.VintageDiscoverer.collectTestClasses(VintageDiscoverer.java:61)
at org.junit.vintage.engine.discovery.VintageDiscoverer.discover(VintageDiscoverer.java:51)
at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:65)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:130)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:117)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:82)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:48)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.support.filter.ExclusionReasonConsumingFilter
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 11 more
其实实际原因是,这个版本的springboot此时默认的是JUnit5,而@RunWith是junit4里面的东西。解决方式有两种,要么你不要用这个@RunWith注解,要么你把pom文件里面的那个org.junit.vintage注释掉。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>org.junit.vintage</groupId>-->
<!--<artifactId>junit-vintage-engine</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
</dependency>
完整代码可看