java - Junit5 与 IntelliJ 和 Gradle

标签 java intellij-idea junit

尝试使用 IntelliJ 2017.2

将我的项目迁移到 java8 + Junit5

我已经添加了junit-jupiter-api版本5.0.0-M6

junit-platform-launcher版本1.0.0-M6

项目结构是默认的maven约定src/test/java

找到几篇关于此的文章,但没有一篇能解决我的问题。

它在控制台中运行良好,我认为这与 IntelliJ 默认的 JUnit Runner 有关,或者我缺少一些依赖项?


当我运行单个测试类时一切正常,但是当我像以前一样选择目录并 Run all 'Tests in Java 时,我遇到了一些错误。

WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.engine.TestDescriptor.pruneTree()V

Aug 02, 2017 2:44:56 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List;

Aug 02, 2017 2:44:56 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.engine.TestDescriptor.pruneTree()V

Aug 02, 2017 2:44:56 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List;

注意:我还没有迁移任何测试,都是 Junit 4 语法。

最佳答案

添加特定的依赖项可以解决问题。

注意:在 2017.2.0 以上更新 INTELLIJ,因为 JUNIT 启动器存在错误

OXYGEN 如果您使用 Eclipse。


以下依赖项启用 Junit5 参数化测试,可用于代替 DataProvider

"org.junit.jupiter:junit-jupiter-params:5.0.0"
//for JUnit5 parametrized tests.

Junit5 API

"org.junit.jupiter:junit-jupiter-api:5.0.0"
//JUnit5 API

如果您想在不更改语法和导入的情况下运行旧版 JUnit4 测试,则需要。

"org.junit.vintage:junit-vintage-engine:4:12.0"
//for legacy JUnit4 tests

编辑:07/2018 将复古运行者的版本与木星版本匹配


如果您想使用新语法和导入运行 JUnit5 测试,则需要。

"org.junit.jupiter:junit-jupiter-engine:5.0.0"
//for JUnit5 tests

java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List;


启动器

"org.junit.platform:junit-platform-launcher:1.0.0
//to handle default launcher

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;


更多信息如何安装JUnit5


从 Gradle 4.6 版开始,不再需要插件 Gradle 原生支持 Junit5,只需执行以下操作: 并且 Vintage runner 的版本现在与 JUnit 5 版本相同。

dependencies {

    testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
    testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"

    testRuntimeOnly "org.junit.vintage:junit-vintage-engine:$junitVersion"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
}

test {  
    useJUnitPlatform {
        includeEngines 'junit-jupiter', 'junit-vintage'
    }
}

关于java - Junit5 与 IntelliJ 和 Gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45462987/

相关文章:

java - Wildfly Greeter 快速入门项目

interface - 用于远程客户端的 Intellij Idea EJB 库

Java Swing 编程结构 : are listeners supposed to be the source of almost all Swing components?

java - Web应用程序中的多线程

java - 可以反编译Java程序

vim - 用于 Intellij 键盘映射的 Ideavim 插件

Java 调试器 : Is it possible selectively suspend threads?

testing - 在gradle junit测试中使用可执行文件

java - 使用 Mockito,如何在没有该类实例的情况下模拟实例方法的返回值?

java - 在测试中使用 DateTimeUtils.setCurrentMillisFixed 是否安全?