Using main dependencies in integration test in Gradle jvm test suite plugin

1.1k views Asked by At

I am having a issue with gradle 8.0.2 using JVM Test Suite plugin. The integration test cannot get the dependencies from the main project.

The build.gradle looks like this. The integration test classes give compilation error as it can't get the dependencies from main project using 'implementation project()

    id 'java'
    id 'java-library'
}



group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.fasterxml.uuid:java-uuid-generator:4.1.0'
}
testing {
    suites {
        integrationtest(JvmTestSuite) {
            dependencies {
                implementation project()
            }
        }
    }
}```
1

There are 1 answers

2
Simon Jacobs On

How the Test Suites plugin works

The documentation on the plugin says:

This [implementation project()] dependency provides access to the project’s outputs as well as any dependencies declared on its api and compileOnlyApi configurations.

Therefore, when you depend on implementation project() in a new test suite you will be able to see public types defined in the main source set's code, plus any dependencies declared in the api configuration – but not those declared in implementation.

The logic behind this, it would seem, is that integration tests should only see the same types that an consuming project would by default. This makes sense since they are "integration tests", testing the external, not internal, API of the project. The regular test source can see the regular implementation dependencies if such tests are needed.

Your case

If you do want to override this convention and access an external dependency (in your case java-uuid-generator) in a set of integration tests, I suggest one of the following options:

  1. Change the configuration that dependency is added to to api:
dependencies {
    api 'com.fasterxml.uuid:java-uuid-generator:4.1.0'
}
  1. Make the integration tests' implementation dependency configuration extend the main implementation configuration, thereby picking up the latter's dependencies and not altering the API a consuming project would see:
integrationtest(JvmTestSuite) {
    configurations {
        named(sources.implementationConfigurationName) {
            extendsFrom(getByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME))
        }
    }
    dependencies {
        implementation project()
    }
}
  1. Simply add the dependency directly to the implementation configuration of the integration tests. This might seem odd, but it's consistent with Gradle's logic: the fact the integration tests need the dependency is conceptually different to the fact the main code needs the dependency:
dependencies {
    implementation project()
    implementation 'com.fasterxml.uuid:java-uuid-generator:4.1.0'
}