java - 如何让 Eclipse 2018.09 默认使用 JUnit 4 测试运行器?

标签 java eclipse unit-testing junit junit4

我刚刚升级到 Eclipse 2018.09(该问题也发生在 Eclipse Photon 上),但它不能很好地与使用 JUnit 4 的项目一起使用。新版本似乎使用 JUnit 运行所有测试5 runner 默认情况下,它失败并出现以下错误,因为我在项目的类路径上只有 JUnit 4,而不是 JUnit 5:

Could not run test. No tests found with test runner 'Junit 5'.

java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineExecutionListener
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:59)
    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:442)
    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.EngineExecutionListener
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 12 more

我希望能够方便地在这个项目中运行我的测试而无需升级到 JUnit 的新主要版本,并且有一个我不必不断重复的解决方法.我认为这需要以某种方式更改默认测试运行器。


我发现的解决方法,以及为什么我对它们不满意:

  1. 每个 测试手动更新每个 运行配置以使用JUnit 4 运行程序。我对此并不满意,因为它很烦人并且需要做很多工作。我找不到将 JUnit 运行配置默认值更改为始终使用 JUnit 4 运行程序的方法。有一些关于“原型(prototype)”运行配置的看起来很有前途的东西,但对于 JUnit 配置,所有选项都是灰色的并且不起作用。
  2. 将 JUnit 5 添加到类路径中。这允许 JUnit 5 runner 工作,但我对此并不满意,因为我不应该因为我的 IDE 而被迫升级。

Eclipse Photon 版本信息:

Eclipse Java EE IDE for Web Developers.
Version: Photon Release (4.8.0)
Build id: 20180619-1200
OS: Windows 7, v.6.1, x86_64 / win32
Java version: 1.8.0_171

“检查更新”显示没有要安装的相关包。

Eclipse 2018.09 版本信息:

Eclipse Java EE IDE for Web Developers.
Version: 2018-09 (4.9.0)
Build id: 20180917-1800
OS: Windows 7, v.6.1, x86_64 / win32
Java version: 1.8.0_171

最佳答案

我今天也偶然发现了这个问题。如果在类路径上找到 JUnit 5 依赖项,eclipse 似乎默认使用 JUnit 5 运行器。

对于我的 Maven 项目,调查依赖层次结构表明实际上 JUnit 4 和 JUnit 5 依赖项都在类路径中。 JUnit 4 作为我自己添加的测试依赖项,JUnit 5 作为我从我依赖的另一个库继承的传递依赖项。从该依赖项中排除 JUnit 5 后,eclipse 再次默认使用 JUnit 4 运行程序。

或许您可以检查一下您的项目是否也有您不知道的对 JUnit 5 的不需要的依赖?

关于java - 如何让 Eclipse 2018.09 默认使用 JUnit 4 测试运行器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53049293/

相关文章:

java - 使用 IntelliJ 设置 JOGL 时出现异常

java - JButtons 显示在父面板之外(?)

Android 致命异常 ActivityNotFoundException

c# - 单元测试 IHttpModule 的最佳实践

java - 使用 EMF 解析字符串格式的 XML

java - 从过滤器修改 servlet 的 errorPage 的 HTML

java - 在 Ruby 中加密的字符串给出 : 'BadPaddingException' when decrypted in Java

java - 如何使用 Eclipse Juno 从管理系统连接 MYSQL 数据库?

macos - 如何模拟Safari 10.0?

Angularjs 单元测试 - 将文本添加到文本区域时 ng-disabled 不起作用