Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ can't find classpath test resource

I'm having a problem where IntelliJ 13.1.4, when running a unit test, can't find a ServiceLoader file in the src/test/resources directory of my module.

Please note before answering that I've done all of the following:

  1. The module is a Gradle project, and if I run gradle test the unit test runs fine.
  2. I've run this unit test successfully in IntelliJ in the past, and it found the module. (Maybe an earlier version of IntelliJ?)
  3. I've double checked the IntelliJ module settings and the src/test/resources directory is marked as being a test resources directory. (See screenshot below.)
  4. I'm dumping the unit test class's classloader's classpath in the class; it has the build/classes/test/ and build/classes/java directories, but neither build/resources/test/ nor build/resources/main/.
  5. I've clicked the button to refresh all Gradle projects.

Any ideas on what else could be causing IntelliJ to fail here?


Screenshot of my module configuration, showing the Test Resource Folders setting.

My Test Resource Folders is correctly set.

like image 788
Luis Casillas Avatar asked Aug 26 '14 19:08

Luis Casillas


People also ask

Where is my classpath in IntelliJ?

find your project or sub-module and click on it to highlight it, then press F4, or right click and choose "Open Module Settings" (on IntelliJ 14 it became F12) click on the dependencies tab. Click the "+" button on the right and select "Jars or directories..." Find your path and click OK.

How do you get the path of SRC test resources directory in JUnit?

File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file. getAbsolutePath(); System.


2 Answers

I managed to get it working better by creating a test-resources directory parallel to the test directory (!).

Still seeing intermittent problems with this, though. Those times, it's possible to open the IntelliJ project settings and define the test resources as source instead (the Gradle project will remain unchanged).

Must be an IntelliJ bug, as I've experienced the same thing in Android Studio.

EDIT:

I found a workaround to the issue.

Add the following at the end of the Gradle config (and specify your test resources path):

task copyTestResources(type: Copy) {     from "${projectDir}/src/test/resources"     into "${buildDir}/classes/test" } processTestResources.dependsOn copyTestResources 
like image 169
Kristofer Sommestad Avatar answered Oct 10 '22 19:10

Kristofer Sommestad


I face a similar issue with IntelliJ 14. Despite having the necessary files in my resources folder and ensuring that the folder is specified as a resource in the module settings, IntelliJ intermittently fails to load my files onto the classpath.

One workaround is to add the required folder as a dependency for the module in the module settings.

enter image description here

First, click on the "+" button to add a new dependency.

enter image description here

Then, select the folder you wish to add to the classpath.

enter image description here

Then select the "classes" option.

enter image description here

Once that is done, re-build your module once and hopefully that should do the trick.

Ideally this should not at all be an issue. All files in the resource folder should directly be put in the classpath. However, every IDE has it's quirks.

like image 38
Aditya Satyavada Avatar answered Oct 10 '22 20:10

Aditya Satyavada