Using android gradle + dagger to run instrumental tests

I started using Android Studio and gradle recently for Android development and found it much better than eclipse / ant or maven. However, I recently started trying to implement some unit tests and integration with my application. I was able to get basic tests using the Espresso scheme recently released by google. I had some tests, although I had to taunt and introduce mocking versions of objects. I used to use a dagger for another project, so I included a dagger in my project. However, now my tests will not run due to the following error:

gradle connectedCheck 

...

4.1.2 failed: toolkit launch failed due to "java.lang.IllegalAccessError": EspressoApp: connectedCheck

I created a simple demo of this here: https://github.com/mwolfe38/android-espresso-dagger

Just a clone and then from the command line: gradle connectedCheck

In the above example, I tried the dependencies in several different ways, initially 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 it gives me an error regarding static initialization. This is apparently caused by some static initialization code in the espresso structure relative to the dagger. So, after adding the dagger dependencies to the InstrumentTompCompile tool, I got the above IllegalAccessError.

Does anyone have luck, including a dagger in your project and espresso tests?

+6
source share
2 answers

Came quite a while, but I finally started to work. I had to do the following:

  • Declare my dependencies 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 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 hamcrest containers from here

  • Delete the license files from such cans (or you will receive an error message 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 notes:
- Hamcrest 1.3 did not work for me, I made a mistake about the coincidence - Crazy, how many hoops I had to slip through to get here.
- Good luck with playing well in android studio.

+11
source

Ok, so I have been dealing with this problem for hours, and here is my fix: Put this depending on your build.gradle

 compile(project(':commons:statemachine')) { exclude module: 'junit' exclude module: 'json' exclude module: 'guava' } compile 'com.google.guava:guava:15.0' instrumentTestCompile files('libs/espresso-1.0-SNAPSHOT-bundled.jar') instrumentTestCompile 'com.squareup:fest-android:1.0.+' 

Add the can of espresso included in the libs folder of your test. Now the important part.

Open this can of espresso complete with WinRar or equivalent and go to the com / folder, then select the android and Delete it folder. Now close WinRar and compile and run your test :-)

0
source

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


All Articles