Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...