IntelliJ can't find classpath test resource
Solution 1
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
Solution 2
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.
First, click on the "+" button to add a new dependency.
Then, select the folder you wish to add to the classpath.
Then select the "classes" option.
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.
Solution 3
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:
- Right click on resources folder > Mark directory as ... > Unmark as Resources Root
- Right click on resources folder > Mark directory as ... > Test Resources Root
Solution 4
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.
Solution 5
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.
Comments
-
Luis Casillas over 3 years
I'm having a problem where IntelliJ 13.1.4, when running a unit test, can't find a
ServiceLoader
file in thesrc/test/resources
directory of my module.Please note before answering that I've done all of the following:
- The module is a Gradle project, and if I run
gradle test
the unit test runs fine. - I've run this unit test successfully in IntelliJ in the past, and it found the module. (Maybe an earlier version of IntelliJ?)
- 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.) - I'm dumping the unit test class's classloader's classpath in the class; it has the
build/classes/test/
andbuild/classes/java
directories, but neitherbuild/resources/test/
norbuild/resources/main/
. - 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.
- The module is a Gradle project, and if I run
-
bacar over 9 yearsHave 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.
-
Kristofer Sommestad over 9 years@bacar Looks like similar issue's already been reported: youtrack.jetbrains.com/issue/IDEA-128273
-
Luis Casillas over 9 yearsI did not get a chance to test your proposed solution, because somehow I can no longer reproduce my original problem.
-
JCricket about 8 yearsAs an update, the above noted IDEA bug is showing as a duplicate, primary bug is: youtrack.jetbrains.com/issue/IDEA-91952
-
Rig almost 8 yearsExperiencing 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.
-
Kristofer Sommestad almost 8 yearsSorry, but no... :(
-
Matthew Chen about 7 yearsThanks the fix worked! This seems still broken as of the date I am writing this comment.
-
Adil Hussain almost 7 yearsThis 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! -
Aditya Satyavada over 6 years@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.
-
saurzcode almost 5 yearsI am still facing this issue, haven't been resolved till now.
-
John Arrowwood over 4 yearsThis works, but my project is SBT, and when the SBT file is re-imported, the change is lost. :(
-
redochka over 3 yearsI 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