Skip to content
Advertisement

Eclipse: Gradle source folder migration breaks JUnit test resources

I refactored an Eclipse project with using Java 11.0.10 to match Gradle’s default source and resource folders instead of eclipse’s source and resource folders, like this:

JavaScript

I moved the sources and resources to the folders accordingly.

When I access a test resource within a JUnit test, like TestClass.class.getResource("/my/package/mytestresource.xml") it returns [ProjectFolder]/src/main/resources/my/package/mytestresource.xml and not [ProjectFolder]/src/test/resources/my/package/mytestresource.xml. The test runs over Eclipse’s JUnit launcher.

TestClass.java is located in [ProjectFolder]/src/test/my/package and the test resource in [ProjectFolder]/src/test/resources/my/package/mytestresource.xml.

This worked perfectly before the refactoring.

How can I tell Eclipse and JUnit to merge the output paths together? Or can I tell Eclipse to work with 2 output paths?

Previously the build.gradle contained this (which was removed completely):

JavaScript

.classpath before changes:

JavaScript

.classpath after changes:

JavaScript

I use the following software:

JavaScript

Advertisement

Answer

I found two workarounds for this issue:

  1. Adding the gradle eclipse plugin to the project and manipulating the classpathentry in the .classpath file of the project:

    JavaScript

or alternatively

  1. Re-introducing of the sourceSets in the gradle build with Gradle’s default source and resource folders.

    JavaScript

I’m not sure why this happens with eclipse, but at least it works again.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement