I have a project built with Gradle version 6.4 and JDK 8. I'm trying to use the Gradle plugin for Test Fixtures (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:
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?
Most important concept in the Gradle
java-test-fixtures
plugin is stated in their documentation:This plugin will indeed create the following dependencies:
main
<--testFixtures
, andtestFixtures
<--test
In your case,
testFixtures
module should automatically depend onmain
sources, and also onmain
dependencies declared inapi
scope (com.my.extenal.project:1.0
)See a similar example in a valid sample project here https://github.com/mricciuti/so-64133013 :
Person
class from main modulemain
dependencies declared inapi
configurationNote that
testFixtures
will not inherit dependencies from thetest
module: if you need to use such libraries in this module (eg. JUnit, Mockito, ...) you will need to declare explicit dependency , usingtestFixturesImplementation
ortestFixturesApi
configuration.See example in
core-module