Test runtime class path does not include test source set's compile classpath


(rainer.frey) #1

I found an awkward behavior for Java unit tests:

My project has a custom source set (named “report”) with library classes that are needed to compile and run the main and test source sets. To achieve compilation, I added the report source set’s output to the main and test sour sets’ compile classpath.

Quite unintuitively I get 'NoClassDefFoundError’s for classes from “report” when executing the unit tests. I did nothing specifically to add the “report” source set to the test task’s class path, but would have expected that the compile class path is automatically on the runtime class path (with thorough reading I see that the documentation does not support that expectation, but I am still surprised).

Is this actually intended? Or did I miss something in my build script to get the expected behavior? Or should I really add the “report” classes to the runtime class path as well as the compile classpath? And what would be the most appropriate level (source set runtimeClasspath, or test task classpath)?

Here’s the relevant part of the build script:

sourceSets {
 report
 main {
  compileClasspath += report.output
 }
 test {
  compileClasspath += report.output
 }
}
  configurations {
        provided
 reportCompile.extendsFrom(compile)
 testRuntime.extendsFrom(provided)
}
  sourceSets.all {compileClasspath += configurations.provided}

(Peter Niederwieser) #2

Or should I really add the “report” classes to the runtime class path as well as the compile classpath?

Yes. A source set’s runtime class path does not automatically “extend” the compile class path.

And what would be the most appropriate level (source set runtimeClasspath, or test task classpath)?

‘sourceSet.runtimeClasspath’ is more appropriate. If you have the choice, always prefer configuring the source set over configuring individual tasks.


(rainer.frey) #3

Thanks, got it. Support on this forum really is extraordinary!