Skip to content
Advertisement

Add provided dependency to test classpath using Gradle

I’ve provided dependency scope configured like below. My problem is, the provided dependencies are not visible during runtime in tests. How can I configure this to keep the dependencies provided but available on the test classpath?

apply plugin: 'java'

configurations {
    provided
}

sourceSets {
    main {
        compileClasspath += configurations.provided
    }
}

dependencies {
    provided 'com.google.guava:guava:18.0'
    provided 'org.apache.commons:commons-lang3:3.3.2'

    // Tests
    testCompile 'junit:junit:4.11'
    testCompile 'org.assertj:assertj-core:1.7.0'

    // Additional test compile dependencies
    testCompile 'joda-time:joda-time:2.2'
}

One solution is to add the dependency like the joda-time library with testCompile scope, but I don’t want to duplicate any entries. I’m sure it can be achieved with proper configuration.

Advertisement

Answer

Two ways to do this. First, have the testRuntime configuration extend from provided.

configurations {
    provided
    testRuntime.extendsFrom(provided)
}

Second, you could add the provided configuration to the classpath of your test task.

test {
    classpath += configurations.provided
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement