如何在Gradle中使用jUnit 5

问题描述 投票:2回答:1

运行IDEA IDE我想为jUnit v5添加一个gradle依赖项。

这是我的build.gradle文件,我使用this答案作为指南。

apply plugin: 'java'
sourceCompatibility = 1.8

repositories { mavenCentral() }
apply plugin: 'org.junit.platform.gradle.plugin'

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'junit:junit:4.12'

    testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")

    testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M4")

    // Enable use of the JUnitPlatform Runner within the IDE
    testCompile("org.junit.platform:junit-platform-runner:1.0.0-M4")
    compile ("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}

junitPlatform {
    details 'tree'
}

这里的问题是通过导入解析了jUnit4注释,但是没有解析所有v5注释。

一个例子:

@ParameterizedTest
public void testExample() {
    // My annotations is not resolved
}

使用gradle添加jUnit5依赖项的正确方法是什么?

编辑我从头开始一个新的gradle java项目到底。

这是我当前的build.gradle。

group 'com.iay0361'
version '1.0-SNAPSHOT'

apply plugin: 'java'
sourceCompatibility = 1.8

repositories { mavenCentral() }
apply plugin: 'org.junit.platform.gradle.plugin'

dependencies {
    testCompile group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '4.12.0-RC3'

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-RC3'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-RC3'
    compile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.0-RC3'

    testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-RC3'
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}

junitPlatform {
    details 'tree'
}

我在测试的新类文件中编写了@Test注释,之后它要求我“将'jUnit5'添加到类路径中

enter image description here

我做了,这次选择了复制'jUnit5'库而不是使用IDEA经销商。

现在它在模块中添加了这些文件:

enter image description here

该文件仍然是RC2,但在build.gradle中它是RC3。 “外部库”目录中也没有jUnit jar

我错过了什么,问题仍然是IDE无法解析某些jUnit5注释,如@ParamiterizedTest。

java junit
1个回答
2
投票

Here是关于如何使用junit5配置gradle的快速示例。在依赖项中,删除junit:4.12工件verison的compile语句。

// If you also want to support JUnit 3 and JUnit 4 tests
testCompile("junit:junit:4.12")

buildscript()方法中,包括以下内容:

buildscript {   
  repositories { mavenCentral() }
  dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3'  } 
}
© www.soinside.com 2019 - 2024. All rights reserved.