Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No ressources availbable when running unit test

There's a known bug in Android Studio where the app's ressources aren't available from within test classes.

The solution according to aforementioned thread is to put the following lines into the 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 have to put the line

processTestResources.dependsOn copyTestResources

within my build.gradle?

EDIT:

I didn't have apply plugin: 'java' in my build files yet. The problem is that in my app module's build gradle, I already have apply plugin: 'android' and trying to add the java plugin leads 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 build file and then

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

Note that I added the modules directory to the from-line. This builds fine, but unit tests that need to access ressources still don't work.

like image 602
fweigl Avatar asked Oct 19 '22 20:10

fweigl


1 Answers

I was unaware that you were using the android plugin, sorry. You're getting the error because the Android plugin defines a different set of build tasks compared to the Java plugin. So you need to pick a different build task to depend your copyTestResources task on, 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 can be a little simpler like so:

task copyTestResources() {
    from sourceSets.test.resources
    into sourceSets.test.output.classesDir
}
like image 100
Hanno Avatar answered Oct 27 '22 19:10

Hanno