Our project uses Cordova which has customized the build.gradle file in the following way:
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
}
I read the documentation on setting up Instrument tests and configured my gradle file as directed:
dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile project(':CordovaLib')
// other deps
// !! THIS IS THE INSTRUMENT SETUP!
// Required -- JUnit 4 framework
androidTestCompile 'junit:junit:4.12'
// Optional -- Mockito framework
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.android.support:support-annotations:23.3.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Optional -- UI testing with UI Automator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}
I've sync'd the gradle and the external libs folder does have all the junit and hamcrest and mockito files. Further proof is given that it has autocomplete when I go to import them into my test class (which is set correctly at src\androidTest\java
:
package co.appname.app.Utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.*;
public class ColorsTest
{
@Test
public void test()
{
assertThat(2, is(2));
}
}
But when I go to compile or run the test I get
Error:(5, 17) error: package org.junit does not exist
Error:(6, 24) error: package org.junit.runner does not exist
Error:(7, 27) error: package org.mockito.runners does not exist
Error:(9, 24) error: package org.hamcrest does not exist
I set up a new project (and didn't modify the source sets) and my Instrumentation tests run just fine.
I figure something must be up with my source sets. So I read the docs on setting up a gradle file and the testing section in particular.
The only thing I could find relevant is this paragraph:
As mentioned previously, next to the main sourceSet is the androidTest sourceSet, located by default in src/androidTest/. Using this sourceSet a test APK gets built, which can be deployed to a device to test the application using the Android testing framework. This can contain android unit tests, instrumentation tests, and uiautomator tests. The node of the manifest for the test app is generated but you can create a src/androidTest/AndroidManifest.xml file to add other components to the test manifest.
However, I tried setting that in my gradle file like such:
androidTest.setRoot('src/androidTests/java')
Didn't work
androidTest.setRoot('tests')
Didn't work
androidTest.setRoot('androidTests')
Didn't work
My project cannot seem to find the junit files and I suspect it has something to do with my source sets being moved (because as I said my test project did run the instrumentation tests) but I have no clue what to do to fix it. If you have any advice (even a cryptic idea of where I might be able to look) I would greatly appreciate it!