Dan Getz
Dan Getz

Reputation: 9152

How can I use Mockito in my Android app itself? I'm receiving Hamcrest dependency conflicts that I don't receive when it's used only in tests

I'd like to use Mockito's mock() and clone() methods in the release version of an app I'm writing, not just in the tests. (Yes, I know what tests are, I'm not worried about performance, and I have a reason for this.) But when I include Mockito as a run-time dependency, my project does not compile. It compiles fine as a test dependency in app/build.gradle, using Mockito 1.10.19, Dexmaker 1.4, and Espresso 2.2.2:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 25
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}
dependencies {
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'

    testCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile 'com.crittercism.dexmaker:dexmaker:1.4'
    androidTestCompile 'com.crittercism.dexmaker:dexmaker-dx:1.4'
    androidTestCompile 'com.crittercism.dexmaker:dexmaker-mockito:1.4'
}

But when I try to change these to run-time dependencies like so:

compile 'org.mockito:mockito-core:1.10.19'
compile 'com.crittercism.dexmaker:dexmaker:1.4'
compile 'com.crittercism.dexmaker:dexmaker-dx:1.4'
compile 'com.crittercism.dexmaker:dexmaker-mockito:1.4'

Gradle returns the following error trying to build the project with the build or assembleAndroidTest tasks, during the prepareDebugUnitTestDependencies task:

Conflict with dependency 'org.hamcrest:hamcrest-core'. Resolved versions for app (1.1) and test app (1.3) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

I can see in the dependencies Gradle task that Mockito 1.10.19 depends on Hamcrest 1.1, and JUnit 4.12 depends on Hamcrest 1.3, but this error is odd to me for two reasons:

  1. I didn't get this error when they were testCompile and androidTestCompile dependencies, but they were all being used then. It seems to me like I didn't add any new test dependencies, so why do they conflict now?
  2. assembleRelease and assembleDebug complete without any errors.

What can I do to use Mockito in both the tests and the release build of my app?

I tried a couple things, with no success:

Upvotes: 1

Views: 358

Answers (1)

Dan Getz
Dan Getz

Reputation: 9152

I was able to get this to work by using older versions of some dependencies: JUnit 4.10 and Espresso 2.1. This changes the dependencies that were for Hamcrest 1.3 to Hamcrest 1.1, so that everyone's using 1.1. On my real project (not the test example in my question), I also had to use the junit-dep artifact instead of the junit one. (This is probably because of a difference between the JUnit artifacts for versions 4.10 and 4.11+, where the junit:4.10 artifact includes some Hamcrest classes in it that the junit-dep:4.10 and junit:4.11+ artifacts don't.) So the dependencies become:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.1', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit-dep:4.10'

compile 'org.mockito:mockito-core:1.10.19'
compile 'com.crittercism.dexmaker:dexmaker:1.4'
compile 'com.crittercism.dexmaker:dexmaker-dx:1.4'
compile 'com.crittercism.dexmaker:dexmaker-mockito:1.4'

Upvotes: 2

Related Questions