eclipse - 由于 NoClassDefFoundError:TestEngine,Junit 5 测试无法在 Eclipse 中启动
问题描述
(在stackoverflow中这个问题没有问题,所以我决定在这里分享问题和解决方案。)
我想将我的 Spring Boot 项目从 JUnit 4 迁移到 JUnit 5,所以我添加了新的 API 作为依赖项并删除了旧的:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
我想对我的项目进行完全迁移,所以我明确排除了 JUnit 4 中的所有内容,并且也没有包括(junit-vintage-engine
如官方迁移指南中所建议的那样)。
然后,我进行了各种更改以使测试编译,例如替换@Before
和@BeforeEach
组织导入。总而言之,这非常简单。
但是在 Eclipse 中运行测试引起了一些麻烦:
- 首先,我收到消息“在项目构建路径上找不到'junit.framework.TestCase'。只有当 JUnit 在构建路径上时,才能运行 JUnit 3 测试。” 在自动弹出的启动配置对话框中,我发现了我的错误:我需要选择 'Test runner'
JUnit 5
。 - 仍然没有成功。现在我收到消息“没有使用测试运行器 'JUnit 5' 找到测试。这令人困惑,因为我在测试方法上使用了正确的 JUnit 5 注释。Eclipse 甚至证实了这一点,因为启动配置'测试方法'搜索列出了测试方法就好了。
过了一会儿,我发现测试执行在控制台中打印了一个堆栈跟踪:
java.lang.NoClassDefFoundError: org/junit/platform/engine/TestEngine
at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:35)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:87)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:67)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:34)
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:423)
at java.lang.Class.newInstance(Class.java:456)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:370)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:365)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:309)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:224)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:208)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.TestEngine
at java.net.URLClassLoader.findClass(URLClassLoader.java:444)
at java.lang.ClassLoader.loadClass(ClassLoader.java:486)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:378)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
... 14 more
这指出了问题的根本原因(见下面的答案)......
解决方案
问题是我缺少对引擎库的依赖:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
然后,测试就像在 Eclipse 中的魅力一样运行。
像https://dev.to/martinbelev/how-to-enable-junit-5-in-new-spring-boot-project-29a8这样的迁移博客确实提到了这种依赖关系,但我主要使用的是JUnit 5 文档,并且不知何故,我一定忽略了那里的这条重要信息......
推荐阅读
- excel - VBA ArrayFormula 太长
- android - CERT.RSA 或类似文件未包含在 meta-inf 文件夹中,是否有可以存储和查看证书的替代位置
- ios - 是否有更快的方法来更改 iOS 模拟器上的可访问性(动态类型)文本大小?
- c# - 搜索键 C# 后显示排序列表中的值
- python - 如何使用递归找到系列 1+ (1*2) + (1*2*3) ... (1*2*3*...n) 的总和?
- laravel - 如何在 Bootstrap 数字按钮/徽章上创建动态数字?
- python - 如何通过 Launch.json 传递参数以在 Visual Studio 2019 中进行调试
- sql - 雪花选择子句中的子查询不逐行评估?
- r - xts 图中的文本注释
- wordpress - http 到 https – 无法访问 wp-admin