java 在 Gradle 环境中访问 JUnit 测试中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27046642/
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
Access file in JUnit test in Gradle environment
提问by Niklas
Right now I have got a Java library which has a test class. In that class I want to access some files located on my hard disk.
现在我有一个 Java 库,它有一个测试类。在该课程中,我想访问位于硬盘上的一些文件。
The build.gradle looks like this:
build.gradle 看起来像这样:
apply plugin: 'java'
dependencies {
testCompile 'junit:junit:4.11'
}
My file is under java_lib/src/test/assets/file.xml
and the Java class is under java_lib/src/test/java/<package_name>.java
我的文件在下面java_lib/src/test/assets/file.xml
,Java 类在下面java_lib/src/test/java/<package_name>.java
Therefore I execute
因此我执行
final InputStream resourceAsStream = this.getClass().getResourceAsStream("assets/file.xml");
final InputStream resourceAsStream = this.getClass().getResourceAsStream("assets/file.xml");
Unfortunately I get null back. What am I doing wrong?
不幸的是,我得到了空值。我究竟做错了什么?
回答by Niklas
To get thing rolling you need to add the following to the gradle file:
为了让事情顺利进行,您需要将以下内容添加到 gradle 文件中:
task copyTestResources(type: Copy) {
from "${projectDir}/src/test/resources"
into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyTestResources
What it basically does is copying all the files in the src/test/resource
directory to build/classes/test
, since this.getClass().getClassLoader().getResourceAsStream(".")
points to build/classes/test
.
它的主要作用是将目录中的所有文件复制src/test/resource
到build/classes/test
,因为this.getClass().getClassLoader().getResourceAsStream(".")
指向build/classes/test
.
The issueis already known to Google and they want to fix it in Android Studio 1.2 (since they need IntelliJ14 for that and it seems like it will be included in Android Studio 1.2)
谷歌已经知道这个问题,他们希望在 Android Studio 1.2 中修复它(因为他们需要 IntelliJ14 并且它似乎将包含在 Android Studio 1.2 中)
回答by Invisible Arrow
Try placing file.xml
under src/test/resources
and use this.getClass().getResourceAsStream("file.xml")
(without the folder prefix)
试着将file.xml
下src/test/resources
和使用this.getClass().getResourceAsStream("file.xml")
(不带文件夹前缀)
The problem appears to be that the assets
folder is not part of the test runtime classpath, hence this.getClass().getResourceAsStream("assets/file.xml")
wouldn't be able to resolve the path as you expected.
问题似乎是该assets
文件夹不是测试运行时类路径的一部分,因此this.getClass().getResourceAsStream("assets/file.xml")
无法按预期解析路径。
By default, the test resources folder in a Gradle java project is src/test/resources
(same as a Maven java project). You can override it to assets
folder if you wish by adding this in the project's build.gradle
file:
默认情况下,Gradle java 项目中的 test resources 文件夹是src/test/resources
(与 Maven java 项目相同)。assets
如果您愿意,可以通过在项目build.gradle
文件中添加以下内容将其覆盖到文件夹:
sourceSets.test {
resources.srcDirs = ["src/test/assets"]
}
回答by ToYonos
In build.gradle
, add this :
在build.gradle
,添加这个:
sourceSets.test {
resources.srcDirs = ["src/test"]
}
In your code, access your resource like this :
在您的代码中,像这样访问您的资源:
getClass().getClassLoader().getResourceAsStream("assets/file.xml"));
Works for me.
对我来说有效。
回答by Thorre
Thanks for pointing out the Google issueI've been looking all day for this...
感谢您指出我一整天都在寻找的 Google问题...
In "Android Studio 1.1 RC 1" (gradle build tool 1.1.0-rc1) there is no need to add the work around to the gradle file, but your you have to execute the test from the gradle task menu (or command prompt)!
在“Android Studio 1.1 RC 1”(gradle 构建工具 1.1.0-rc1)中,无需将解决方法添加到 gradle 文件中,但您必须从 gradle 任务菜单(或命令提示符)执行测试!
回答by DALDEI
This worked for me (3 years later, gradle 4.10)
这对我有用(3 年后,gradle 4.10)
subprojects {
junitPlatformTest.dependsOn processTestResources
}