67

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.

Luis Casillas
  • 29,802
  • 7
  • 49
  • 102
  • 3
    Resource patterns in the compiler dialog may be? – matt helliwell Aug 26 '14 at 19:55
  • Good idea! I just checked that, and I have `!?*.java;!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj`. I tried adding my filename to that, but it made no difference; the classpath printed when the test runs still doesn't include the `src/test/resources/` dir. – Luis Casillas Aug 26 '14 at 21:09
  • 3
    I having the exactly same issue here... Intellij 13.1.4 – Pedro Dusso Sep 11 '14 at 19:19
  • What's your code for loading the resource? – bacar Sep 24 '14 at 19:03
  • @bacar: I don't load it myself directly; it's done by a library that I use ([junit-quickchek](https://github.com/pholser/junit-quickcheck)). But I do know that the library uses the Java platform's [`ServiceLoader`](http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html) class. – Luis Casillas Sep 24 '14 at 20:10
  • Do you have dynamic.classpath set to true? I have discovered similar problems (with a maven project) where the classpath is not set correctly for some classloaders when this is on. Setting is in .idea/workspace.xml – bacar Sep 26 '14 at 13:38
  • Well, I can no longer reproduce the issue, so the value of this answer is questionable, but I just checked and the property is false. – Luis Casillas Sep 26 '14 at 18:17
  • I had a maven project with the same problem, In my case it was solved by changing the working directory configuration. Here are 2 configurations you should check in intellij: https://devnet.jetbrains.com/message/5534500#5534500 – Alex Pi Feb 09 '15 at 18:30
  • Same problem on IntelliJ IDEA 2018.3.2 / Ultimate – beetstra Dec 20 '18 at 11:43

9 Answers9

35

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
Kristofer Sommestad
  • 3,061
  • 27
  • 39
  • Have you reported this to Jetbrains support? They're fairly responsive. I would have thought that a correct implementation of gradle support should not require explicitly copying the resources. – bacar Sep 26 '14 at 13:40
  • 7
    @bacar Looks like similar issue's already been reported: https://youtrack.jetbrains.com/issue/IDEA-128273 – Kristofer Sommestad Sep 29 '14 at 10:11
  • I did not get a chance to test your proposed solution, because somehow I can no longer reproduce my original problem. – Luis Casillas Sep 30 '14 at 18:21
  • As an update, the above noted IDEA bug is showing as a duplicate, primary bug is: https://youtrack.jetbrains.com/issue/IDEA-91952 – JCricket May 18 '16 at 04:19
  • 2
    Experiencing the same with Maven. Still. Any idea what the maven equivalent of your fix would be? I have tests that depend on some flat files and Maven finds them in the console but IntelliJ explodes claiming not to find them. – Rig Jul 29 '16 at 23:13
  • Sorry, but no... :( – Kristofer Sommestad Jul 31 '16 at 19:35
32

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.

Aditya Satyavada
  • 1,028
  • 1
  • 10
  • 14
  • 4
    Thanks the fix worked! This seems still broken as of the date I am writing this comment. – Matthew Chen May 17 '17 at 23:38
  • 1
    This does work but the problem with this in a Gradle project is that any changes made to the module's setup will be lost the next time a Gradle sync happens (and thus the modules in the project are all refreshed). If there was a way to make this change by means of the module's `build.gradle` file that would be great! – Adil Hussain Aug 09 '17 at 11:24
  • 1
    @AdilHussain I was using maven for this. However, my unit tests were working by running maven using command prompt, but were failing when running via intelliJ. What I faced was IDE specific. Not build tool specific. – Aditya Satyavada Jan 09 '18 at 19:21
  • I am still facing this issue, haven't been resolved till now. – saurzcode Jul 23 '19 at 22:26
  • This works, but my project is SBT, and when the SBT file is re-imported, the change is lost. :( – John Arrowwood Sep 19 '19 at 14:27
18

When I was running into the same issue, it worked for me to once unmark the resources directory as resources root and then mark it again as resources root:

  1. Right click on resources folder > Mark directory as ... > Unmark as Resources Root
  2. Right click on resources folder > Mark directory as ... > Test Resources Root
  • 1
    I confirm this hack does the trick. But I'd appreciate if anyone can explain why is intellij IDEA doing this in 2020. Tested on 2020.3 EAP – redochka Sep 25 '20 at 12:25
6

In my case, I simply ran the tests through maven command line, i.e. a basic mvn clean install. After that it worked in IDEA.

manyways
  • 4,386
  • 2
  • 23
  • 19
2

When there is a multi-module project, IntelliJ tends to set the top module as the working directory. If this this the case, one can try changing it to the sub-module directory.

Zaziro
  • 405
  • 4
  • 8
1

If your project is multi maven and other answers did not work, try this one.

Change the working directory in intellij. Right Click, go to edit configuration, select the required maven project directory you want to run.

enter image description here

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
1

I recently encountered this where the junit test could not locate a resource in the src/test/resources directory.

I had a stray <packaging>pom</packaging> in the pom.xml file and taking it out solved it in my case.

jdex
  • 1,279
  • 1
  • 13
  • 20
  • Moving the code into a sub-module instead of the parent aggregator module worked for me https://stackoverflow.com/a/18735578 – ahiijny Aug 25 '23 at 20:40
0

For me, Build -> Recompile created out folder described in Project Structure. then it could find my classes. enter image description here

Miae Kim
  • 1,713
  • 19
  • 21
-1

I ran into the same issue, where Junit was failing(FileNotFound) because I had space in my root project folder. eg: my project/project1/....

  • I removed the space from the root folder, my_project/project1/ all test passed properly.