No resources available when running unit test

There is a known bug in Android Studio, where application resources are not available from the test classes.

The solution according to the above thread should contain the following lines in build.gradle:

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

but gradle says:

 Error:(132, 0) Could not find property 'processTestResources' on project ':app'. 

Where exactly do I need to put the line

 processTestResources.dependsOn copyTestResources 

inside my build.gradle?

EDIT:

I have not had apply plugin: 'java' in my build files yet. The problem is that in the module of my app gradle module I already have apply plugin: 'android' and try to add java plugins to

 Error:The 'java' plugin has been applied, but it is not compatible with the Android plugins. 

I tried puttin apply plugin: 'java' in the top level assembly file and then

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

Notice that I added the module directory to the line. This is great, but unit tests that require access to resources still do not work.

+5
source share
2 answers

I did not know that you are using the android plugin, sorry. You get an error because the Android plugin defines a different set of build tasks compared to the Java plugin. Therefore, you need to choose a different build task to depend on your copyTestResources task, for example:

 apply plugin: 'android' // (...) more build logic task copyTestResources() { from "${projectDir}/src/test/resources" into "${buildDir}/classes/test" } check.dependsOn copyTestResources 

Also, your copyTestResources task might be a little easier:

 task copyTestResources() { from sourceSets.test.resources into sourceSets.test.output.classesDir } 
+3
source

Good workaround for working: https://github.com/nenick/AndroidStudioAndRobolectric/blob/master/app/build.workaround-missing-resource.gradle

With this, you can abandon your additional task, just add the associated β€œ.gradle” file and apply it in your build.gradle modules.

 apply from: 'build.workaround-missing-resource.gradle' 

Be sure to place your assets in the application / test / resources / and specify them only by their name.

for example: this.getClass().getClassLoader().getResource("my-asset-file.json")

I found a solution in the ticket you specified: https://code.google.com/p/android/issues/detail?id=64887

0
source

Source: https://habr.com/ru/post/1208888/


All Articles