java - 使用 Maven 的命名约定在 Gradle 中进行集成测试?

标签 java maven gradle integration-testing

来自 Maven,我正在探索 Gradle 作为替代方案。技术:Java 11、jUnit 5、Maven 3.6、Gradle 5.6。

我陷入了配置集成测试的困境。遵循 Maven 的 Surefire 和 Failsafe 插件的默认命名约定,我的测试位于标准测试目录中,并通过后缀进行区分:单元测试以 Test.java 结尾,集成测试以 IT 结尾.java.

是否可以在 Gradle 中进行相同的设置?到目前为止我见过两种方法:

  • 使用 jUnit5 的标签(这意味着我必须去标记每个集成测试)
  • 使用单独的目录进行单元测试和集成测试

理想情况下,我希望保持文件夹结构不变,因为它会影响多个 git 存储库。

最佳答案

好的,我想我成功地让它与源集一起工作,感谢 Lukas 的反馈和 Slaw .

如果可以改进,请告诉我:

// define the dependencies of integrationTest, inherit from the unit test dependencies
configurations {
    integrationTestImplementation.extendsFrom(testImplementation)
    integrationTestRuntimeOnly.extendsFrom(testRuntimeOnly)
}

sourceSets {
    test {
        java {
            // exclude integration tests from the default test source set
            exclude "**/*IT.java"
        }
    }

    // new source set for integration tests
    integrationTest {
        // uses the main application code
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
        java {
            // the tests are at the same directory (or directories) as the unit tests
            srcDirs = sourceSets.test.java.srcDirs

            // exclude the unit tests from the integration tests source set
            exclude "**/*Test.java"
        }
        resources {
            // same resources directory (or directories) with the unit tests
            srcDirs = sourceSets.test.resources.srcDirs
        }
    }
}

// define the task for integration tests
task integrationTest(type: Test) {
    description = "Runs integration tests."
    group = "verification"
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    shouldRunAfter test

    // I'm using jUnit 5
    useJUnitPlatform()
}

// make the check task depend on the integration tests
check.dependsOn integrationTest

关于java - 使用 Maven 的命名约定在 Gradle 中进行集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58645456/

相关文章:

java - 在 WSL 和 Windows 上安装 SDK(JDK、Maven、Gradle)?

python - 为可移植运行者构建 apache beam sdk 线束 - 名称问题

javascript - 如何在ajax url中传递多个数组值

java - x-power-by 在响应 header 中显示

java - Maven:如果项目正在发布或未发布,则需要一种方法来更改属性或 URL 值

maven - 如何构建WSO2存储库?

gradle - 无法使用 gradle 从 nexus 下载 ojdbc 特定 jar

java - 如何打印给定数字 n 的魔术矩阵最多 n 方

java - 如何从 xls 文件生成 xml 或 json 映射,然后从该映射生成 html 表(结构)?

java - 如何在 Eclipse 中将 Gradle 转换为 Maven