我正在进行多模块 gradle 项目。在我的一个模块中,我有 api 依赖性:
api('de.flapdoodle.embed:de.flapdoodle.embed.mongo')
我想将其更改为在所有模块的测试中可见的依赖关系。有
testImplementation
依赖性,但没有 testApi
。
我不能再依赖生产类路径,因为我想使用真正的 mongo 实例而不是嵌入式实例。另一方面,我在依赖于数据访问的不同模块中进行了测试 - 在这种情况下,我想在测试类路径上使用嵌入式 mongo 来运行这些测试。
如何使此依赖关系在所有模块测试中可见?
问题(在我看来)是在多模块项目中跨模块共享测试代码
简短的回答 - 否 - 跨模块有直接的测试依赖共享。
通过构建设置在模块之间共享测试代码
官方gradle路线https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures
简单破解
testImplementation files(project(':core-module').sourceSets.test.output.classesDirs)
将上述行单独添加到您需要的位置,或者在适当的条件下使用
subprojects()
添加到根目录中
*还有其他可能的路线*
例如:通过配置
child.testImplementation extends parent.testImplementation (or runtime)
感谢@prasadu 的回答:
有能力
testFixtures
:
https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures
如果您将它们与
*api
一起使用,就像在 core
模块中一样:
testFixturesCompileOnlyApi 'asdf:qwer:zxcv'
testFixturesApi 'org.springframework.boot:spring-boot-starter-test'
testFixturesRuntimeOnly 'org.junit.platform:junit-platform-launcher'
然后如果您
use
其他模块中的核心模块,例如:
implementation project(':core')
testImplementation testFixtures(project(':core'))
您可以自动使用该测试