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

huangapple go评论72阅读模式
英文:

Gradle test fixtures plugin and core module dependencies

问题

我有一个项目,使用的是Gradle 6.4版本和JDK 8。我试图使用Gradle的[Test Fixtures插件][1](`java-test-fixtures`),但是在依赖方面遇到了一些问题。

根据上面链接的Gradle页面,项目的结构应该如下所示:

core-module
-- src
-- main
-- java
-- test
-- java
-- testFixtures
-- java


而`build.gradle.kts`文件中的依赖部分如下:

```kotlin
dependencies {
    api("com.my.external.project:1.0")
    // ... 更多的API依赖

    testFixturesCompileOnly(project(":core-module"))
    testFixturesApi("junit:junit:4.12")
    // ... 更多的测试依赖
}

现在,在我使用的IDE(IntelliJ)中,testFixtures/java源文件夹中的类可以看到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模块中声明的依赖项?


<details>
<summary>英文:</summary>

I have a project built with Gradle version 6.4 and JDK 8. I&#39;m trying to use the Gradle [plugin for Test Fixtures][1] (`java-test-fixtures`) but I have some issues with the dependencies.

According to the Gradle page linked above, the project should be structured like this:

core-module
-- src
-- main
-- java
-- test
-- java
-- testFixtures
-- java


While the `build.gradle.kts` file has the following dependencies section:

```kotlin
dependencies {
    api(&quot;com.my.external.project:1.0&quot;)
    // ... more API dependencies

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

Now, in IntelliJ (the IDE I'm using) classes in the testFixtures/java source folder see the classes in the main/java source folder. So I can add new Java classes under testFixtures/java that have dependencies on those under main.
However, I won't be able to import the dependencies from the external library com.my.external.project:1.0. The problem is confirmed when I try to run the Gradle task compileTestFixturesJava.

I can duplicate the entry in the dependencies section; e.g. I can add:

testFixturesImplementationOnly(&quot;com.my.external.project:1.0&quot;)

But that is not really what I expect to do; especially when I have dozens of dependencies.

I could also define the dependencies in an array and run a for-each over them. Still, this is not the cleanest solution.

Is there a clean solution that will allow the testFixtures module to use the dependencies declared in the main module?

答案1

得分: 16

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

> [该插件] 将自动创建一个testFixtures源集,您可以在其中编写测试固件。测试固件的配置如下:
> - 它们可以看到主源集类
> - 测试源可以看到测试固件类

该插件确实会创建以下依赖关系:main <-- testFixtures,以及 testFixtures <-- test

在您的情况下,testFixtures模块应自动依赖于main源,还应依赖于api范围中声明的main依赖项 (com.my.extenal.project:1.0)

在此处的有效示例项目中查看类似示例:https://github.com/mricciuti/so-64133013 :

  • Simpsons 类可以访问来自主模块的 Person
  • TestHelpers 类可以访问在 api 配置中声明的 main 依赖项

请注意,testFixtures 不会继承 test 模块的依赖项:如果您需要在此模块中使用这些库(例如 JUnit、Mockito 等),您需要声明显式依赖,使用 testFixturesImplementationtestFixturesApi 配置。

core-module 中查看示例

plugins {
    id ("java-library")
    id ("java-test-fixtures")
}
dependencies {
    // 主要依赖项
    //   也将在 "testFixture" 中可用,感谢 "api" 配置
    api("org.apache.commons:commons-lang3:3.9")
    api(project(":utils-module"))

    // 测试固件依赖项
        // ==> mockito lib 将在 testFixture 模块中可用,也可在使用模块(例如 test)中可用
    testFixturesApi("org.mockito:mockito-core:3.5.13")

    // 测试依赖项
       // 特定于 "test" 模块的依赖项;从 "testFixtures" 不可见
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.3.1")
    testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.1")
}
英文:

Most important concept in the Gradle java-test-fixtures plugin is stated in their 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

This plugin will indeed create the following dependencies: main <-- testFixtures , and testFixtures <-- test

In your case, testFixtures module should automatically depend on main sources, and also on main dependencies declared in api scope ( com.my.extenal.project:1.0)

See a similar example in a valid sample project here https://github.com/mricciuti/so-64133013 :

  • Simpsons class has access to Person class from main module
  • TestHelpers class has access to main dependencies declared in api configuration

Note that testFixtures will not inherit dependencies from the test module: if you need to use such libraries in this module (eg. JUnit, Mockito, ...) you will need to declare explicit dependency , using testFixturesImplementation or testFixturesApi configuration.

See example in core-module

plugins {
    id (&quot;java-library&quot;)
    id (&quot;java-test-fixtures&quot;)
}
dependencies {
    // Main dependencies
    //   will be available in &quot;testFixture&quot; as well thanks to &quot;api&quot; configuration
    api(&quot;org.apache.commons:commons-lang3:3.9&quot;)
    api(project(&quot;:utils-module&quot;))

    // Testfixture dependencies
        // ==&gt; mockito lib will be available in testFixture module and also in consumer modules (e.g test)
    testFixturesApi(&quot;org.mockito:mockito-core:3.5.13&quot;)

    // Test dependencies
       // dependencies specific to the &quot;test&quot; module; not visible from &quot;testFixtures&quot;
    testImplementation(&quot;org.junit.jupiter:junit-jupiter-api:5.3.1&quot;)
    testRuntimeOnly (&quot;org.junit.jupiter:junit-jupiter-engine:5.3.1&quot;)
}

huangapple
  • 本文由 发表于 2020年9月30日 15:41:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64133013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定