我有一个使用 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 :
Person
来自主模块的类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/