Java IntelliJ 找不到类路径测试资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25513788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ can't find classpath test resource
提问by Luis Casillas
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.
我遇到了一个问题,IntelliJ 13.1.4 在运行单元测试时无法ServiceLoader
在src/test/resources
我的模块目录中找到文件。
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.
- 该模块是一个 Gradle 项目,如果我运行
gradle test
单元测试运行良好。 - 我过去在 IntelliJ 中成功运行了这个单元测试,它找到了模块。(也许是 IntelliJ 的早期版本?)
- 我已经仔细检查了 IntelliJ 模块设置,并且该
src/test/resources
目录被标记为测试资源目录。(见下面的截图。) - 我在类中转储单元测试类的类加载器的类路径;它有
build/classes/test/
和build/classes/java
目录,但没有build/resources/test/
和build/resources/main/
。 - 我点击了按钮来刷新所有 Gradle 项目。
Any ideas on what else could be causing IntelliJ to fail here?
关于还有什么可能导致 IntelliJ 在这里失败的任何想法?
Screenshot of my module configuration, showing the Test Resource Folders setting.
我的模块配置的屏幕截图,显示了测试资源文件夹设置。
采纳答案by Kristofer Sommestad
I managed to get it working better by creating a test-resources
directory parallel to the test
directory (!).
通过创建一个test-resources
与test
目录 (!)平行的目录,我设法让它更好地工作。
Still seeing intermittent problems with this, though. Those times, it's possible to open the IntelliJ project settings and define the test resources as sourceinstead (the Gradle project will remain unchanged).
尽管如此,仍然会遇到间歇性问题。那些时候,可以打开 IntelliJ 项目设置并将测试资源定义为源(Gradle 项目将保持不变)。
Must be an IntelliJ bug, as I've experienced the same thing in Android Studio.
一定是 IntelliJ 错误,因为我在 Android Studio 中遇到过同样的事情。
EDIT:
编辑:
I found a workaroundto the issue.
Add the following at the end of the Gradle config (and specify your test resources path):
在 Gradle 配置的末尾添加以下内容(并指定您的测试资源路径):
task copyTestResources(type: Copy) {
from "${projectDir}/src/test/resources"
into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyTestResources
回答by Aditya Satyavada
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.
我在 IntelliJ 14 中遇到了类似的问题。尽管我的资源文件夹中有必要的文件并确保在模块设置中将该文件夹指定为资源,但 IntelliJ 间歇性地无法将我的文件加载到类路径中。
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.
理想情况下,这根本不应该成为问题。资源文件夹中的所有文件都应该直接放在类路径中。然而,每个 IDE 都有它的怪癖。
回答by akshaymittal143
I ran into the same issue, where Junit was failing(FileNotFound) because I had space in my root project folder. eg: my project/project1/....
我遇到了同样的问题,Junit 失败(FileNotFound),因为我的根项目文件夹中有空间。例如:我的项目/项目1/....
- I removed the space from the root folder, my_project/project1/ all test passed properly.
- 我从根文件夹中删除了空间, my_project/project1/ 所有测试都正确通过。
回答by Henning Heitk?tter
When I was running into the same issue, it worked for me to once unmarkthe 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
- 右键单击资源文件夹 > 将目录标记为 ... > 取消标记为资源根目录
- 右键单击资源文件夹 > 将目录标记为 ... > 测试资源根
回答by manyways
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.
就我而言,我只是通过 maven 命令行运行测试,即一个基本的mvn clean install
. 之后它在IDEA中工作。