Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring gradle to use JUnit, Mockito, Hamcrest and Espresso

I have an Android project that uses Mockito, Hamcrest and Espresso to help with testing.

No matter what I try with the Gradle build file, I get a NoSuchMethodError for org.hamcrest.Matcher.anyOf when I try to run my tests after doing gradle androidTestCompile.

Here is my current configuration:

dependencies {
    compile project(':GameCore')
    androidTestCompile files(
            'libs/espresso-1.1.jar',
            'libs/testrunner-1.1.jar',
            'libs/testrunner-runtime-1.1.jar'
    )
    androidTestCompile(
            'junit:junit:4.11',
            'org.mockito:mockito-core:1.10.0',
            'com.google.guava:guava:14.0.1',
            'com.squareup.dagger:dagger:1.1.0',
            'com.google.dexmaker:dexmaker:1.0',
            'com.google.dexmaker:dexmaker-mockito:1.0',
            'org.hamcrest:hamcrest-core:1.3',
            'org.hamcrest:hamcrest-library:1.3'
            )

I've tried rewriting the Mockito and JUnit requirements to exclude Hamcrest like so:

androidTestCompile('junit:junit:4.11') {
    exclude group: 'org.hamcrest'
}

But this doesn't make any difference.

The GameCore project is a pure Java project. It also has dependencies on JUnit and Mockito, but as they're specified as testCompile, I don't think they should be interfering.

The output for gradle dependencies for this module for 'androidTestCompile` is:

+--- junit:junit:4.11                                                               
|    \--- org.hamcrest:hamcrest-core:1.3
+--- org.mockito:mockito-core:1.10.0
|    +--- org.hamcrest:hamcrest-core:1.1 -> 1.3
|    \--- org.objenesis:objenesis:2.1
+--- com.google.guava:guava:14.0.1
+--- com.squareup.dagger:dagger:1.1.0
|    \--- javax.inject:javax.inject:1
+--- com.google.dexmaker:dexmaker:1.0
+--- com.google.dexmaker:dexmaker-mockito:1.0
|    +--- com.google.dexmaker:dexmaker:1.0
|    \--- org.mockito:mockito-core:1.9.5 -> 1.10.0 (*)
+--- org.hamcrest:hamcrest-core:1.3
\--- org.hamcrest:hamcrest-library:1.3
     \--- org.hamcrest:hamcrest-core:1.3

Edit

Having further investigated the problem, I see that espresso needs Hamcrest 1.1, but I'm also using assertThat, which is in Hamcrest 1.3. Hamcrest 1.3 doesn't have the anyOf method that espresso uses. So I guess I'm stuck :)

like image 409
Pikaling Avatar asked Nov 05 '14 20:11

Pikaling


Video Answer


2 Answers

I realised that assertThat is in Hamcrest 1.1, it's just in hamcrest-integration instead of hamcrest-core. I changed my build file and it's all working now:

androidTestCompile files(
        'libs/espresso-1.1.jar',
        'libs/testrunner-1.1.jar',
        'libs/testrunner-runtime-1.1.jar'
)
androidTestCompile(
        'org.mockito:mockito-core:1.9.5',
        'com.google.dexmaker:dexmaker-mockito:1.0',
        'com.google.dexmaker:dexmaker:1.0',
        'com.google.guava:guava:14.0.1',
        'com.squareup.dagger:dagger:1.1.0',
        'org.hamcrest:hamcrest-core:1.1',
        'org.hamcrest:hamcrest-integration:1.1',
        'org.hamcrest:hamcrest-library:1.1'
)

I tried using espresso-1.1-bundled.jar but that caused dex errors because two copies of Hamcrest 1.1 were pulled in, so I'd have had to exclude it from a bunch of dependencies.

like image 195
Pikaling Avatar answered Nov 11 '22 21:11

Pikaling


I am currently using all of those libraries in my Android Gradle project (I REALLY like testing and TDD as well :) ).

Here is the dependencies section of my build.gradle file:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleDependency
    compile 'com.google.android.gms:play-services:5.0.89'
    provided 'com.squareup.dagger:dagger-compiler:1.2.1'
    compile 'com.jakewharton:butterknife:5.1.2'
    compile 'com.squareup.dagger:dagger:1.2.1'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
    compile 'com.squareup.retrofit:retrofit:1.6.0'
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.squareup:otto:1.3.5'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile 'com.google.code.findbugs:jsr305:1.3.9'
    compile 'com.j256.ormlite:ormlite-android:4.43'
    compile 'com.j256.ormlite:ormlite-core:4.43'
    compile 'com.android.support:support-v13:20.0.0'
    compile 'com.path:android-priority-jobqueue:1.1.2'
    compile'com.squareup.picasso:picasso:2.3.3'
    compile 'com.github.johnkil.android-robototextview:robototextview:2.1.0'
    compile 'se.emilsjolander:stickylistheaders:2.5.0'
    compile 'com.newrelic.agent.android:android-agent:4.+'

    compile 'com.github.chrisbanes.actionbarpulltorefresh:library:0.9.9'

    //mockito dependencies
    androidTestCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile files('libs/dexmaker-mockito-1.0.jar')
    androidTestCompile files('libs/dexmaker-1.0.jar')

    //espresso dependencies
    androidTestCompile 'com.google.guava:guava:18.0'
    androidTestCompile 'com.squareup.spoon:spoon-client:1.1.1'
    androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') {
        exclude group: 'com.squareup.dagger'
    }
    compile('com.crashlytics.sdk.android:crashlytics:2.0.0@aar') {
        transitive = true;
    }
}

A lot of these you can probably ignore so here is a list with just the specific ones to testing:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':facebook')
    compile project(':library')
    //noinspection GradleDependency

    //mockito dependencies
    androidTestCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile files('libs/dexmaker-mockito-1.0.jar')
    androidTestCompile files('libs/dexmaker-1.0.jar')

    //espresso dependencies
    androidTestCompile 'com.google.guava:guava:18.0'
    androidTestCompile 'com.squareup.spoon:spoon-client:1.1.1'
    androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') 
}

And the dexmaker libs can be found here: https://code.google.com/p/dexmaker/downloads/detail?name=dexmaker-1.0.jar&can=2&q=

and here:

https://code.google.com/p/dexmaker/downloads/detail?name=dexmaker-mockito-1.0.jar&can=2&q=

like image 44
Andre Perkins Avatar answered Nov 11 '22 21:11

Andre Perkins