How to NOT copy/load classes from the test resources folder?

I have observed that .class files found in folders beneath the /src/test/resources tree are copied both to /build/resources/test and build/classes/test folders. I’m assuming this behavior is intentional. How can I disable that behavior?

It is a Java (multi) project with Gradle 2.3.

If you are wondering why I need this: I am working on a library that will dynamically setup new classloaders to load code authored by our end-users. So I have .class files as resources for my unit tests and they should NOT be copied into /build/classes/test and the copies in /build/resources/test should NOT be included in the classpath. You can imagine my surprise when my unit tests passed before I had written the implementation :open_mouth:


How are you using these files? Anything in the ‘resources’ folder is going to be placed on the testing classpath by default. You can exclude these files, but then the question remains, how do you intend to access them from your unit tests?

That is a good question. I had hoped to be able to access the path to the source folders, either through a Gradle attribute or an environment variable.

Is there another layout supported by Gradle for unit test data that does not appear in the classpath?

You can put them anywhere really. From your test you can simply access the file using new File("src/test/resources/Foo.class"). As I see it your options are:

  1. Exclude the class files from the test source set: sourceSets.test.resources.exclude '**/*.class'
  2. Place the files in a location other than ‘src/test/resources’.
  3. Rename the files with a custom extension (ex. Foo.class.test) and load the files from the test classpath using getResourceAsStream().

If possible, I’d recommend #3.

Thanks for your reply.

Choice #3 would then not be an accurate test of real-world conditions, so I don’t feel like that is our best option.

So I’m trying out #2 - putting them in a different location. I think that is working well enough for us.

Thanks for your assistance!