解决springboot2.2.1 单元测试报错NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory

操作系统:window10  

开发工具:Spring Tool Suite - Eclipse Oxygen.3a (4.7.3a)

springboot版本:


		org.springframework.boot
		spring-boot-starter-parent
		2.2.1.RELEASE
		 
	

 

使用 Spring Starter Project 快速方式 创建的项目,自带的测试类

解决springboot2.2.1 单元测试报错NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory_第1张图片

右键Run as--> Junit Test  ,执行报错 no tests found with test runner 'Junit5'

查看控制台输出

java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.(JUnit5TestLoader.java:31)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
	at java.lang.Class.newInstance(Class.java:438)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:367)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:362)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:306)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:221)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:205)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.core.LauncherFactory
	at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 11 more

网上搜索各种帖子,给出的解决方案 很多:让 修改  junit的版本到4、在@SpringBootTest 后面关联上 启动类、添加@Runwith注解等等,均没有用。

最后看到了一篇分析贴,有段知识挺好,分享给大家:

JUnit Platform是提供了运行(测试框架)环境的平台,JUnit Jupiter 是新的Junit5(子项目提供了一个基于平台测试运行Jupiter的测试引擎),JUnit Vintage提供了Junit3/4的测试引擎(应该是向前兼容的意思)

最后,在一个外文网站上找到了解决方案:

You are missing JUnit 5 platform launcher with group: 'org.junit.platform', name: 'junit-platform-launcher' .Gradle dependency: testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.1.1'

添加上  junit-platform-launcher  对应的依赖


	org.junit.platform
	junit-platform-launcher

解决springboot2.2.1 单元测试报错NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory_第2张图片

再次执行,成功!

 

你可能感兴趣的:(springboot)