【问题标题】:How to write a Java integration test task in Gradle with JUnit5?如何使用 JUnit5 在 Gradle 中编写 Java 集成测试任务?
【发布时间】:2017-09-19 21:47:39
【问题描述】:

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

【问题讨论】:

  • JUnit5 只为main 源集定义了一个任务。您可以在junit-team/junit5-samples 上查看此类支持的请求。您将需要一个新任务来运行 JUnit。您可以在this answer 中查看如何设置它的示例。我认为 JUnit 团队不打算支持它,正在等待 the Gradle team 构建原生支持。
  • @mkobit 我知道这是 gradle 问题。谢谢链接,我想我快到了

标签: java gradle junit5


【解决方案1】:

我最终删除了插件并直接调用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

【讨论】:

    猜你喜欢
    • 2019-08-19
    • 2022-06-23
    • 2017-10-30
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多