[英]Gradle test fixtures plugin and core module dependencies
我有一个使用 Gradle 6.4 版和 JDK 8 构建的项目。我正在尝试将 Gradle 插件用于测试夹具( 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
}
现在,在的IntelliJ(我使用的IDE)中的类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
模块中声明的依赖项?
Gradle java-test-fixtures
插件中最重要的概念在他们的文档中说明:
[这个插件] 将自动创建一个 testFixtures 源集,您可以在其中编写您的测试夹具。 测试夹具配置为:
- 他们可以看到主要的源集类
- 测试源可以看到测试装置类
这个插件确实会创建以下依赖项: main
<-- testFixtures
和testFixtures
<-- test
在您的情况下, testFixtures
模块应自动依赖于main
来源,以及在api
范围( com.my.extenal.project:1.0
)中声明的main
依赖项
在此处查看有效示例项目中的类似示例https://github.com/mricciuti/so-64133013 :
Person
类api
配置中声明的main
依赖项请注意, testFixtures
不会从test
模块继承依赖项:如果您需要在此模块中使用此类库(例如 JUnit、Mockito 等),您将需要使用testFixturesImplementation
或testFixturesApi
配置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")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.