JUnit单元测试报错NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory

2022-11-30 16:04:50 浏览数 (1)

环境

IDEA 2019.3 OS:macOS 15

在 test 目录下执行单元测试时报错

代码语言:javascript复制
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-platform-launcher 依赖即可。

代码语言:javascript复制
<dependency>
	<groupId>org.junit.platform</groupId>
	<artifactId>junit-platform-launcher</artifactId>
</dependency>

原因

因为 JUnit 5 = JUnit Platform JUnit Jupiter JUnit Vintage

  • JUnit Platform是提供了运行(测试框架)环境的平台
  • JUnit Jupiter 是新的Junit5(子项目提供了一个基于平台测试运行Jupiter的测试引擎)
  • JUnit Vintage提供了Junit3/4的测试引擎(向前兼容) 所以这里报错的原因应该是:缺少对应依赖()。

0 人点赞