-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mockito 2.x on Android Instrumentation Tests with Kotlin is not working #1082
Comments
I think there are two ways. Kotlin all-open pluginThis plugin makes classes annotated with a specific annotation open without the First, you put these files into your project:
package your.app
@Target(AnnotationTarget.ANNOTATION_CLASS)
annotation class OpenClass
package your.app
@OpenClass
@Target(AnnotationTarget.CLASS)
annotation class OpenClassOnDebug
package your.app
@Target(AnnotationTarget.CLASS)
annotation class OpenClassOnDebug Second, configure your buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
}
apply plugin: 'kotlin-allopen'
allOpen {
annotation 'your.app.OpenClass'
} Finally, annotate @OpenClassOnDebug
class YourClass Now you can open classes and members only on debug mode. DexOpenerThis library opens classes and members while testing on Android. To use this library, configure your android {
defaultConfig {
minSdkVersion 16 // 16 or higher
testInstrumentationRunner 'com.github.tmurakami.dexopener.DexOpenerAndroidJUnitRunner'
}
}
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
dependencies {
androidTestCompile 'org.mockito:mockito-android:2.8.9'
androidTestCompile 'com.github.tmurakami:dexopener:0.9.2'
} |
Thank you @tmurakami, The first solution worked as I wanted. |
@tmurakami Using your DexOpener plugin:
Since Mockito-android is so large, I had to use LinkedIn's dexmaker. |
What version of DexOpener are you using? |
@tmurakami Sure. I am trying that now, but now the test app reaches multidex. Do you multidex for your test apks? |
@tmurakami When turning on multidex:
|
I'd like to investigate it, so please file a report in the DexOpener issues. |
@tmurakami done. |
Curious, is there anything in the works to allow for easier mocking in Android Instrumentation tests besides the two approached above? I see the "cannot use the inline mock maker on Android due to limitations", but just wondering if some other workaround will be part of Mockito one day. |
@GEllickson Honestly, what I have found is that Espresso should only be asserting the views have the proper data and visibility. Id add cases, I have used stubs or local mock data. |
The DexOpener library worked great for me, thank you @tmurakami for creating it! |
Hi 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:
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 ??
The text was updated successfully, but these errors were encountered: