如何使用 JUnit5 在 Gradle 中编写 Java 集成测试任务?

How to write a Java integration test task in Gradle with JUnit5?

对于 Junit4,我对集成测试的定义如下:

task testIntegration(type: Test, dependsOn: jar) {
    group 'Verification'
    description 'Runs the integration tests.'
    testLogging {
        showStandardStreams = true
    }

    testClassesDirs = sourceSets.testInt.output.classesDirs
    classpath = sourceSets.testInt.runtimeClasspath
    systemProperties['jar.path'] = jar.archivePath
}

但是,对于 JUnit5,这不再有效。我不知道要更改什么(为时已晚)。有什么提示吗?

我正在使用 junit-platform-gradle-plugin

我最终删除了插件并直接调用 ConsoleRunner

task testIntegration(type: JavaExec, dependsOn: jar) {
    group 'Verification'
    description 'Runs the integration tests.'

    dependencies {
        testRuntime lib.junit5_console
    }

    classpath = sourceSets.testInt.runtimeClasspath
    systemProperties['jar.path'] = jar.archivePath

    main 'org.junit.platform.console.ConsoleLauncher'
    args = ['--scan-classpath', sourceSets.testInt.output.classesDirs.asPath,
            '--reports-dir', "${buildDir}/test-results/testInt"
    ]
}

另外,这里是如何应用 JaCoCo 的设置:

afterEvaluate {
    jacoco {
        applyTo testUnit
        applyTo testIntegration
    }
    testIntegration.extensions.getByName("jacoco").excludes = ['*Test*', '*.?', '*Foo*', 'jodd.asm5.*', '*.fixtures.*']
}

有关更多详细信息,请查看 Jodds build file