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.

Kotlin + MockIto + Android Instrumentation Test

See original GitHub issue

I am trying to run an Instrumentation test (androidTest) using Mockito in Android with Kotlin.

  • I’ve the core library in my dependencies. 'org.mockito:mockito-core:3.3.3'
  • My project is written in Kotlin so I’ve org.mockito:mockito-inline:3.3.3 to fix the final class issue.
  • As I want to run the mocks in Android, I’ve org.mockito:mockito-android:3.3.3 also in my dependencies.

but am getting below error while compiling the code

More than one file was found with OS independent path ‘mockito-extensions/org.mockito.plugins.MockMaker’

Here’s a sample test code to reproduce the issue. Run this test as an Instrumentation test. (androidTest).

MainActivityTest

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations

class Calculator {
    fun add(x: Int, y: Int): Int {
        return x + y
    }
}

@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    @Mock
    lateinit var calculator: Calculator

    @Before
    fun before() {
        MockitoAnnotations.initMocks(this)
    }

    @Test
    fun test() {
        `when`(calculator.add(10, 10)).thenReturn(20)
        assertEquals(calculator.add(10, 10), 20)
    }
}

app/build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "com.theapache64.mockitosample"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'org.mockito:mockito-core:3.3.3'
    androidTestImplementation 'org.mockito:mockito-android:3.3.3'
    androidTestImplementation 'org.mockito:mockito-inline:3.3.3'
}

Isn’t it possible to run Android instrumentation test using Kotlin with Mockito ?

NOTE: If you think this is a duplicate question, I’ve searched all over the internet. There are other issues with similar stack-trace, but my case is different.

Any help would be highly appreciated

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
autorunman22commented, Mar 21, 2021

This works for me androidTestImplementation 'org.mockito:mockito-core:3.3.3' androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.28.0'

3reactions
plastivcommented, Nov 8, 2020

why this is closed? exists a solution for this? or, mockito is just for unit testing without the android said of things? i think you should update the documentation specifying that.

Android ships with forked java virtual machine tailored for resource limited mobile environments. Historically, Google Android team didn’t invest much into keeping android vm on par with desktop jvm. Up until android P (api 28, 2018) android virtual machine doesn’t support inlining api. Mockito (or any other mock library) can’t rewrite finals at runtime on such androids due to lack of supported apis.

Which leads us to next cases:

  • use mockito-inline with jvm tests no problem since they run on desktop vm
  • use dexmaker if you run androidTest on api 28+ android devices only
  • open kotlin classes at compilation time with open keyword or all-open annotation (extra plugin) for running androidTest tests on android devices with api 27 and below

Detailed comparison table for the reference: https://mockk.io/ANDROID.html

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin + MockIto + Android Instrumentation Test - Stack Overflow
I am trying to run an Instrumentation test (androidTest) using Mockito in Android with Kotlin . ... Here's a sample test code to...
Read more >
Unit Testing with Mockito on Kotlin Android Project with ...
To explain how test a ViewModel with Mockito I have created a sample application to fetch the user's repositories list, to do that...
Read more >
Unit testing in Kotlin projects with Mockk vs. Mockito
Mockito and Mockk are written in Java and Kotlin, respectively, and since Kotlin and Java are interoperable, they can exist within the same ......
Read more >
[Solved]-Kotlin + MockIto + Android Instrumentation Test-kotlin
Coding example for the question Kotlin + MockIto + Android Instrumentation Test-kotlin.
Read more >
Build instrumented tests | Android Developers
Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of ...
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

No results found

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