java - Gradle 测试夹具插件和核心模块依赖项

标签 java gradle dependency-management gradle-kotlin-dsl java-test-fixtures

我有一个使用 Gradle 版本 6.4 和 JDK 8 构建的项目。我正在尝试使用 Gradle plugin for Test Fixtures ( java-test-fixtures ) 但我对依赖项有一些问题。
根据上面链接的 Gradle 页面,该项目的结构应如下所示:

core-module
-- src
   -- main
      -- java
   -- test
      -- java
   -- testFixtures
      -- java
build.gradle.kts文件具有以下依赖项部分:
dependencies {
    api("com.my.external.project:1.0")
    // ... more API dependencies

    testFixturesCompileOnly(project(":core-module"))
    testFixturesApi("junit:junit:4.12")
    // ... more test dependencies
}

现在,在 testFixtures/java 中的 IntelliJ(我正在使用的 IDE)类中源文件夹见 main/java 中的类源文件夹。所以我可以在 testFixtures/java 下添加新的 Java 类依赖于 main 下的那些.
但是,我将无法从外部库中导入依赖项 com.my.external.project:1.0 .当我尝试运行 Gradle 任务时,问题得到确认 compileTestFixturesJava .
我可以复制 dependencies 中的条目部分;例如我可以补充:
testFixturesImplementationOnly("com.my.external.project:1.0")
但这并不是我真正期望做的。特别是当我有几十个依赖项时。
我还可以在数组中定义依赖关系并运行 for-each在他们之上。不过,这不是最干净的解决方案。
是否有一个干净的解决方案允许 testFixtures模块使用 main 中声明的依赖项模块?

最佳答案

Gradle 中最重要的概念 java-test-fixtures插件在他们的documentation中说明:

[this plugin] will automatically create a testFixtures source set, in which you can write your test fixtures. Test fixtures are configured so that:

  • they can see the main source set classes
  • test sources can see the test fixtures classes

这个插件确实会创建以下依赖项:main <-- testFixtures , 和 testFixtures <-- test在您的情况下,testFixtures模块应该自动依赖 main来源,以及 main api 中声明的依赖项范围(com.my.extenal.project:1.0)
在此处查看有效示例项目中的类似示例 https://github.com/mricciuti/so-64133013 :
  • Simpsons类可以访问 Person来自主模块的类
  • TestHelpers类可以访问 main api 中声明的依赖项配置

  • 请注意 testFixtures不会从 test 继承依赖关系模块:如果您需要在该模块中使用此类库(例如 JUnit、Mockito 等),您需要使用 testFixturesImplementation 声明显式依赖关系或 testFixturesApi配置。
    参见 core-module 中的示例
    plugins {
        id ("java-library")
        id ("java-test-fixtures")
    }
    dependencies {
        // Main dependencies
        //   will be available in "testFixture" as well thanks to "api" configuration
        api("org.apache.commons:commons-lang3:3.9")
        api(project(":utils-module"))
    
        // Testfixture dependencies
            // ==> mockito lib will be available in testFixture module and also in consumer modules (e.g test)
        testFixturesApi("org.mockito:mockito-core:3.5.13")
    
        // Test dependencies
           // dependencies specific to the "test" module; not visible from "testFixtures"
        testImplementation("org.junit.jupiter:junit-jupiter-api:5.3.1")
        testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.1")
    }
    

    关于java - Gradle 测试夹具插件和核心模块依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64133013/

    相关文章:

    java.lang.IllegalStateException : You need to use a Theme. AppCompat 主题(或后代)与此 Activity

    java - 如何在 Java 中启用启用 SecurityManager 的情况下创建 newInstance

    java - 无法从数据流中的 bigtable 读取

    java - Spring 抛出 NoClassDefFoundError : MethodInterceptor although class exists within classpath

    Grails log4j SMTPAppender NoClassDefFoundError

    grails - 如何将软管鸟客户端库集成到Grails项目?

    java - 在运行时更改 Android Camera 2 的 Flash 设置

    android - 错误 :No such property: bootClasspath for class: com. android.build.gradle.LibraryPlugin

    java - 更新现有 Gradle Libs 依赖项,以在 Spring 项目中使用 Jackson 2.8.5 和 DynamoDB 编码 ZonedDateTime

    java - 来自 PMD、checkstyle、findbugs 的报告