question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mockito 2.x on Android Instrumentation Tests with Kotlin is not working

See original GitHub issue

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:

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:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

24reactions
tmurakamicommented, May 16, 2017

I think there are two ways.

  1. Kotlin all-open plugin
  2. DexOpener

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:

  • app/src/ debug /java/your/app/OpenClass.kt
package your.app

@Target(AnnotationTarget.ANNOTATION_CLASS)
annotation class OpenClass
  • app/src/ debug /java/your/app/OpenClassOnDebug.kt
package your.app

@OpenClass
@Target(AnnotationTarget.CLASS)
annotation class OpenClassOnDebug
  • app/src/ release /java/your/app/OpenClassOnDebug.kt
package your.app

@Target(AnnotationTarget.CLASS)
annotation class OpenClassOnDebug

Second, configure your app/build.gradle as follows:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
    }
}

apply plugin: 'kotlin-allopen'

allOpen {
    annotation 'your.app.OpenClass'
}

Finally, annotate OpenClassOnDebug to the class you want to mock:

@OpenClassOnDebug
class YourClass

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:

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'
}
1reaction
tmurakamicommented, Jul 17, 2017

I’d like to investigate it, so please file a report in the DexOpener issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin + MockIto + Android Instrumentation Test - Stack Overflow
I've the core library in my dependencies. 'org.mockito:mockito-core:3.3. · My project is written in Kotlin so I've org.mockito:mockito-inline:3.3 ...
Read more >
Troubleshooting Mockito-Kotlin in Android Testing - mvndy
Scenario 1: Invalid use of matchers? (ノ`Д´)ノ. Mockito is pretty descriptive about this mistake. Be sure you're not combining raw values with ...
Read more >
Writing Espresso instrumentation tests with Dagger2 + Kotlin
Step2: Mocking Kotlin class in Android Test​​ However, we can not mock them as all classes are final by default in Kotlin. Mockito...
Read more >
Set up project for AndroidX Test - Android Developers
Step 1: Open the build.gradle file for your Gradle module. Step 2: In the repositories section, make sure Google's Maven repository appears:.
Read more >
[Solved]-Kotlin + MockIto + Android Instrumentation Test-kotlin
Related Query · Kotlin + MockIto + Android Instrumentation Test · Mockito 2 for Android Instrumentation test : Could not initialize plugin: interface...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found