英文:
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'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("com.my.external.project:1.0")
// ... more API dependencies
testFixturesCompileOnly(project(":core-module"))
testFixturesApi("junit:junit:4.12")
// ... 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("com.my.external.project:1.0")
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 等),您需要声明显式依赖,使用 testFixturesImplementation
或 testFixturesApi
配置。
在 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 inapi
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 ("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")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论