Java – accessing files in JUnit tests in the gradle environment

Now I have a Java library with a test class In this class, I want to access some files located on my hard disk

build. The gradle looks like this:

apply plugin: 'java'

dependencies {
    testCompile 'junit:junit:4.11'
}

My files are in Java_ lib / src / test / assets / file. Under XML, the Java class is located in Java_ lib / src / test / java /< package_ name> . java

So I do

Final InputStream resourceasstream = this getClass(). getResourceAsStream(“assets / file.xml”);

Unfortunately, I got null. What did I do wrong?

Solution

To scroll things, you need to add the following to the gradle file:

task copyTestResources(type: Copy) {
    from "${projectDir}/src/test/resources"
    into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyTestResources

It basically copies all the files in the Src / test / resource directory to build / classes / test because this getClass(). getClassLoader(). getResourceAsStream(“.”) Point to build / classes / test

Issue is known by Google. They want to fix it in Android studio 1.2 (because they need intellij14, it seems that it will be included in Android studio 1.2)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>