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

操作系统:window10  

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

springboot版本:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

 

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

右键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.<init>(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  对应的依赖

<dependency>
	<groupId>org.junit.platform</groupId>
	<artifactId>junit-platform-launcher</artifactId>
</dependency>

再次执行,成功!

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值