Junit 5 tests don't launch in Eclipse due to NoClassDefFoundError: TestEngine
Junit 5 tests don't launch in Eclipse due to NoClassDefFoundError: TestEngine
(在Whosebug中没有对这个问题的提问,所以我决定在这里share提问和解决。)
我想将我的 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
(如官方migration guide).
然后,我进行了各种更改以使测试通过编译,例如将 @Before
替换为 @BeforeEach
并组织导入。总而言之,这非常简单。
但是运行 Eclipse 中的测试造成了一些麻烦:
- 首先我收到消息 "Cannot find 'junit.framework.TestCase' on project build path. JUnit 3 tests can only be run if JUnit is on the build path." 在自动弹出的启动配置对话框中,我发现了我的错误:我需要 select 'Test runner'
JUnit 5
.
- 仍然没有成功。现在我收到消息“没有找到测试运行器 'JUnit 5' 的测试。这很令人困惑,因为我在测试方法上使用了正确的 JUnit 5 注释。Eclipse 甚至证实了这一点,因为启动配置 'Test method' 搜索列出测试方法就好了。
一段时间后,我发现测试执行在控制台中打印了堆栈跟踪:
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 do mention this dependency, but I primarily worked with the JUnit 5 documentation 这样的迁移博客,我一定是忽略了那里的这条重要信息...
(在Whosebug中没有对这个问题的提问,所以我决定在这里share提问和解决。)
我想将我的 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
(如官方migration guide).
然后,我进行了各种更改以使测试通过编译,例如将 @Before
替换为 @BeforeEach
并组织导入。总而言之,这非常简单。
但是运行 Eclipse 中的测试造成了一些麻烦:
- 首先我收到消息 "Cannot find 'junit.framework.TestCase' on project build path. JUnit 3 tests can only be run if JUnit is on the build path." 在自动弹出的启动配置对话框中,我发现了我的错误:我需要 select 'Test runner'
JUnit 5
. - 仍然没有成功。现在我收到消息“没有找到测试运行器 'JUnit 5' 的测试。这很令人困惑,因为我在测试方法上使用了正确的 JUnit 5 注释。Eclipse 甚至证实了这一点,因为启动配置 'Test method' 搜索列出测试方法就好了。
一段时间后,我发现测试执行在控制台中打印了堆栈跟踪:
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 do mention this dependency, but I primarily worked with the JUnit 5 documentation 这样的迁移博客,我一定是忽略了那里的这条重要信息...