Mockito 2.x on Android Instrumentation Tests with Kotlin is not working
See original GitHub issueHi guys,
I am current working on a Android project using the Kotlin language, and I’m trying mock some classes on Instrumentation Tests. As everything in Kotlin is final by default, we have the known problem of mock final classes, that only works if we apply the “mock-maker-inline” on file “org.mockito.plugins.MockMaker” inside test resources folder (eg.: src/test/resources/mockito-extensions/).
My mockito dependencies on module grade file are:
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:1.1.2-3"
...
testCompile "junit:junit:4.12"
testCompile "org.jetbrains.kotlin:kotlin-reflect:1.1.2-3"
testCompile "org.mockito:mockito-core:2.8.9”
testCompile ("com.nhaarman:mockito-kotlin:1.4.0", {
exclude group: 'org.jetbrains.kotlin'
exclude group: 'org.mockito'
})
androidTestCompile "org.mockito:mockito-android:2.8.9”
androidTestCompile ("com.nhaarman:mockito-kotlin:1.4.0", {
exclude group: 'org.jetbrains.kotlin'
exclude group: 'org.mockito'
})
androidTestCompile ("com.android.support.test.espresso:espresso-core:2.2.2", {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile ("com.android.support.test:runner0.5", {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile ("com.android.support.test:rules:0.5", {
exclude group: 'com.android.support', module: 'support-annotations'
})
For the local unit tests, the “mock-maker-inline” works properly. But for Android Instrumentations Tests it doesn’t works. Reading about it on Mockito documentation, we have:
“Be aware that you cannot use the inline mock maker on Android due to limitations in the Android VM”.
We can’t apply the “mock-maker-inline” for Instrumentation tests.
So, how can we enable Mockito to mock final classes on Android Instrumentations Test ??
Issue Analytics
- State:
- Created 6 years ago
- Reactions:17
- Comments:11 (3 by maintainers)
Top GitHub Comments
I think there are two ways.
Kotlin all-open plugin
This plugin makes classes annotated with a specific annotation open without the
open
keyword.First, you put these files into your project:
Second, configure your
app/build.gradle
as follows:Finally, annotate
OpenClassOnDebug
to the class you want to mock:Now you can open classes and members only on debug mode.
DexOpener
This library opens classes and members while testing on Android.
To use this library, configure your
app/build.gradle
as follows:I’d like to investigate it, so please file a report in the DexOpener issues.