IntelliJ找不到类路径测试资源 [英] IntelliJ can't find classpath test resource
问题描述
我遇到一个问题,IntelliJ 13.1.4在运行单元测试时无法在 src中找到
我模块的目录。 ServiceLoader
文件/ test / resources
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:
- 该模块是Gradle项目,如果我运行
gradle test
,单元测试运行正常。 - 我过去在IntelliJ中成功运行了这个单元测试,它找到了模块。 (也许是早期版本的IntelliJ?)
- 我已经仔细检查了IntelliJ模块设置和
src / test / resources
目录被标记为测试资源目录。 (见下面的截图。) - 我在类中转储了单元测试类的类加载器的类路径;它有
build / classes / test /
和build / classes / java
目录,但都没有build / resources / test /
也不是build / resources / main /
。 - 我点击了刷新所有Gradle项目的按钮。
- 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.
关于还有什么可能导致IntelliJ失败的想法?
Any ideas on what else could be causing IntelliJ to fail here?
我的模块配置截图,显示测试资源文件夹设置。
Screenshot of my module configuration, showing the Test Resource Folders setting.
推荐答案
我设法通过创建与 test
test-resources 目录来更好地工作>目录(!)。
I managed to get it working better by creating a test-resources
directory parallel to the test
directory (!).
但仍然看到间歇性问题。那些时候,可以打开IntelliJ项目设置并将测试资源定义为 source (Gradle项目将保持不变)。
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).
必须是IntelliJ错误,因为我在Android Studio中遇到过相同的事情。
Must be an IntelliJ bug, as I've experienced the same thing in Android Studio.
编辑:
我找到解决方法。
在Gradle配置的末尾添加以下内容(并指定您的测试资源路径):
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
这篇关于IntelliJ找不到类路径测试资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!