And I have a simple JUnit test which tests a Servlet. However my project does not compile, because javax.servlet:javax.servlet-api:3.1.0 dependency is not available to compileTestJava task.
considering this was added to handle the use case of maven “provided” artifacts, it’s kinda dumb that it doesn’t actually do so vis-a-vis tests. why that approach ?
We took this approach because we wanted to treat tests as a “consumer” of your production code. In this sense it should inherit dependencies in the same way. That is, since other projects that depend on your code will not see these dependencies, neither should your tests. In many cases this does in fact result in a duplicate dependency declaration but for functional tests you’ll probably want to use a proper implementation dependency instead of the compileOnly version anyhow.
The solution proposed in reply #7 is deprecated in Gradle 6, resulting in the following message on the console:
The compileOnly configuration has been deprecated for resolution. This will fail with an error in Gradle 7.0. Please resolve the compileClasspath configuration instead. Consult the upgrading guide for further information: https://docs.gradle.org/6.8.3/userguide/upgrading_version_5.html#dependencies_should_no_longer_be_declared_using_the_compile_and_runtime_configurations
And as indicated, with Gradle 7, it results in an error:
Resolving dependency configuration 'compileOnly' is not allowed as it is defined as 'canBeResolved=false'.
Instead, a resolvable ('canBeResolved=true') dependency configuration that extends 'compileOnly' should be resolved.
The solution proposed in reply #8 works for me, both in Gradle 6 and 7.