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?
compileTestFixturesJava
fails without a duplicated entry because it (for whatever reason) is not drawing from the already-specified dependencies declared asapi
/implementation
under thedependencies
tag (available tomain
), causing unresolved classes? Also, would you be able to provide a minimalist reproduction repository? – WachtertestFixture
classes can seemain
dependencies (here :commons-lang3) : if you analyse dependencies you will see thattestFixturesCompileClasspath
inherits frommain
dependencies . maybe you could compare this simple project to yours and check what's different? – Chlorothiazidejunit
dependency intestFixturesImplementation
configuration. 2) "nor any code from the main module" ==> you're wrong:Simpsons
class in test Fixture referencesPerson
class frommain
sourcesSet.. – Chlorothiazidemockito
), personally I use testFixtures only for setting up domain entity instances for test purpose, but your use case is perfectly valid. in this case, just declare your needed dependencies intestFixturesImplementation
ortestFixturesApi
scope. regarding dependency fromtestFixture
tomain
sourceSet it's automatically set up by the plugin so you don't need to declare it : I did it just because I copied your initial example – ChlorothiazidesourceSets { testFixtures { compileClasspath += sourceSets.main.output runtimeClasspath += sourceSets.main.output } }
(sorry for the bad formatting) – PostdoctoralsourceSets { … }
“ – @Postdoctoral — Isn't that already being configured implicitly byJavaPlugin.configureSourceSets(JavaPluginConvention, BuildOutputCleanupRegistry)
andJavaTestFixturesPlugin.createImplicitTestFixturesDependencies(Project, JavaPluginConvention)
? – Redmond