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:
- I didn't get this error when they were
testCompile
andandroidTestCompile
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? assembleRelease
andassembleDebug
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:
duplicating the Mockito and Dexmaker dependencies, specifying them both as normal and as test dependencies.
using Mockito 2. I tried the current version 2.2.22, and also 2.1.0, but with either I got a similar error trying to use
mock()
in a JUnit test:java.lang.AbstractMethodError: com.android.dx.mockito.DexmakerMockMaker.isTypeMockable(Ljava/lang/Class;)Lorg/mockito/plugins/MockMaker$TypeMockability; at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29) at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22) at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:168) at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:162) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62) at org.mockito.Mockito.mock(Mockito.java:1637) at org.mockito.Mockito.mock(Mockito.java:1550)
From what I can find online, it looks like there's no current way for Mockito 2.x to work on Android.