Using classloaders in testing

I have currently been working 12 hours on this last bug in migrating from Maven to Gradle, and I hope to get some pointers for a fix here.

From test method:

URL fileResource = CaseReportMock.class.getClassLoader().getResource(JasperConstants.PATH);

fileResouce is null, and I believe getClassLoader() is to blame. As I understand it, Gradle instantiates it’s own JVM for tests, rendering getClassLoader() useless.

I found this solution

FilteringClassLoader filter = getServices().get(ClassLoaderRegistry).gradleApiClassLoader.parent


on the Nabble forum.

I also found the FilteringClassLoader-class on GitHub, but I don’t know how to define this in my project.
I can think of two possible solutions:

  1. Add a modified version of the Nabbler-snippet in my Java testclass, or even in the CaseReportMock class, and define a compiledependency in the main build.gradle like so:

    subproject {
    dependencies {
    compile: ‘org.gradle:internal:2.6’

or 2) As the snippet didn’t have semicolons, I was tempted to think that this should go in the subproject’s build.gradle.

I am totally lost, and growing desperate for help on this issue. I am aware that my definition of the compile dependency is wrong, but I’m not that skilled to just take a resource from GitHub and transform it into a dependency yet. Any pointers in the right direction will be extremely helpful!

Thanks in advance!

Where does JasperConstants.PATH points to ?

Running debug yesterday told me it finds the path at


Edit: I gave the Maven path with target first

I guess you have a ‘test’ sourceSet (or you follow the convention of test being in 'src/test/java’
Gradle automatically copy test resources in test class directory before executing the test task (see the doc on the Java plugin, and the processTestResources task.

Therefore you only need to define the resources of your ‘test’ sourceSet (by default it is ‘src/test/resources’)
What I usually do after, is to follow the same package declaration for my resources as well.
if CaseReportMock is in package some.custom.package

I would put the resource it uses in the same package (therefore in the directory src/test/resources/some/custom/package

After I would retrieve it with CaseReportMock.class..getResource('report.jasper') (see the javadoc on Class)