testImplementation doesn't obtain src/test/java output

I have a very large multi-module project where module A’s test code depends on module B’s test code.

  • A/src/test/java
  • B/src/test/java <== requires classes from A/src/test/java

In the past we’ve created a “test artifact” jar as shown here: How do i declare a dependency on a module's test code?

This works: testCompile project (path: ‘:A’, configuration: ‘testArtifacts’)
This fails: testImplementation project (path: ‘:A’, configuration: ‘testArtifacts’)

I don’t want to completely restructure the project using the new “fixtures”. However, I’d like to migrate to the api/implementation configurations instead of using compile/testCompile for managing dependencies.

What is the preferred way to include a module’s test classes as a dependency for another module?

Hi @blakecmartin,

You can do this by declaring your tests as feature variant:

subprojects {
    plugins.withType(JavaLibraryPlugin) {
        java {
            registerFeature('test') { usingSourceSet(sourceSets.test) }
        }
    }
}

And then declare a dependency on them like this:

    dependencies {
        testImplementation(project(":myOtherProject")) {
           capabilities { requireCapability("project-group:myOtherProject-test") }
        }
    }

You could also write a helper method so you get something similar to testFixtures(...) when declaring dependencies:

def tests(Project project) {
    dependencies.create(project) {
        capabilities { requireCapability("$project.group:$project.name-test") }
    }
}

And then you can do:

dependencies {
    testImplementation(tests(project(":myOtherProject")))
}

More details about feature variants:

(Test fixtures basically use the same mechanism in the background)

1 Like

I tried this solution, it didn’t work for me. I have a large android app, that contains many modules, I have the same exact issue as described. the project has no project group or groupid, if I try this solution gradle throws an error saying that the format is groupid:artificat:version, how could I solve this?

Thanks in advance