I have began using Android Studio and gradle recently for android development and find it much better overall than eclipse/ant or maven. However I've recently began trying to implement some kind of unit and or integration tests with my app. I was able to get basic tests working using the Espresso framework recently released by google. I had some tests though where I needed to mock and inject mocked versions of objects. I used dagger in the past for another project so I included dagger into my project. However now my tests won't run because of the following error:
gradle connectedCheck
...
4.1.2 failed: Instrumentation run failed due to 'java.lang.IllegalAccessError' :EspressoApp:connectedCheck
I created a simple demo of this here: https://github.com/mwolfe38/android-espresso-dagger
Just clone and then from command line run: gradle connectedCheck
In the above I have tried the dependencies several different ways, originally like this:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.squareup.dagger:dagger-compiler:1.1.0'
compile 'com.squareup.dagger:dagger:1.1.0'
instrumentTestCompile files('libs/espresso-1.0-SNAPSHOT.jar',
'libs/testrunner-1.0-SNAPSHOT.jar',
'libs/testrunner-runtime-1.0-SNAPSHOT.jar')
instrumentTestCompile 'org.hamcrest:hamcrest-all:1.3'
instrumentTestCompile 'com.google.guava:guava:15.0'
}
but that gives me an error regarding static initialization. This appears to be caused by some static initialization code in the espresso framework regarding dagger. So After adding dagger dependencies to instrumentTestCompile I get the IllegalAccessError mentioned above.
Anyone have luck including dagger in your project and doing espresso tests?
Took quite awhile but I finally got it working. I had to do the following:
Declare my dependencies like so:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.squareup.dagger:dagger-compiler:1.1.0'
compile 'com.squareup.dagger:dagger:1.1.0'
instrumentTestCompile files('libs/espresso-1.0-SNAPSHOT.jar','libs/testrunner-1.0-SNAPSHOT.jar','libs/testrunner-runtime-1.0-SNAPSHOT.jar')
instrumentTestCompile files('libs/hamcrest-core-1.1.jar', 'libs/hamcrest-library-1.1.jar', 'libs/hamcrest-integration-1.1.jar')
instrumentTestCompile 'com.google.guava:guava:14.0.1'
}
Copy the hamcrest jars from here
Remove the license files from the jars like this (or else you'll get an error about duplicate LICENSE.txt files)
zip -d hamcrest-core-1.1.jar LICENSE.txt
zip -d hamcrest-library-1.1.jar LICENSE.txt
Run gradle connectedCheck
A few things to note:
- Hamcrest 1.3 didn't work for me, got an error about a matcher was missing
- Crazy how many hoops I had to jump through to get here.
- Good luck getting this to play well with android studio.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With