由于NoClassDefFoundError:TestEngine,Junit 5测试无法在Eclipse中启动 [英] Junit 5 tests don't launch in Eclipse due to NoClassDefFoundError: TestEngine

查看:371
本文介绍了由于NoClassDefFoundError:TestEngine,Junit 5测试无法在Eclipse中启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(对于stackoverflow中的这个问题没有疑问,因此我决定在此处 share 问题与解决方案.)

我想将Spring Boot项目从JUnit 4迁移到JUnit 5,所以我添加了新的API作为依赖项,并删除了旧的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(如官方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 确实提到了这种依赖性,但是我主要使用share question & solution here.)

    I wanted to migrate my Spring Boot project from JUnit 4 to JUnit 5, so I added the new API as dependency and removed the old the ones:

       <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>
    

    I wanted to do a full migration of my project, so I explicitly excluded everything from JUnit 4, and also did not include the junit-vintage-engine (as recommended in the official migration guide).

    Then, I did the various changes to make the tests compile, like replacing @Before with @BeforeEach and organizing imports. All in all, this was pretty straightforward.

    But running the tests in Eclipse caused some trouble:

    • First I got the message "Cannot find 'junit.framework.TestCase' on project build path. JUnit 3 tests can only be run if JUnit is on the build path." In the launch configuration dialog that popped up automatically, I was able to spot my mistake: I needed to select the 'Test runner' JUnit 5.
    • Still no success. Now I got the message "No tests found with test runner 'JUnit 5'. This was confusing, because I had used the correct JUnit 5 annotations on the test methods. Eclipse even confirmed this because the launch configuration 'Test method' search listed the test methods just fine.

    After I while, I figured out that the test execution had printed a stack trace in the console:

    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
    

    This pointed me to the root cause of the problem (see answer below)...

    解决方案

    The problem was that I was missing the dependency to the engine library:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
        </dependency>
    

    Then, the tests run like a charm in Eclipse.

    Migration blogs like 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, and somehow I must have overlooked this important piece of information there...

    这篇关于由于NoClassDefFoundError:TestEngine,Junit 5测试无法在Eclipse中启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆