Kotlin + MockIto + Android Instrumentation Test
See original GitHub issueI 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’veorg.mockito:mockito-inline:3.3.3
to fix the final class issue. - As I want to run the mocks in
Android
, I’veorg.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:
- Created 3 years ago
- Reactions:1
- Comments:13 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This works for me
androidTestImplementation 'org.mockito:mockito-core:3.3.3'
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.28.0'
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:
androidTest
on api 28+ android devices onlyopen
keyword orall-open
annotation (extra plugin) for runningandroidTest
tests on android devices with api 27 and belowDetailed comparison table for the reference: https://mockk.io/ANDROID.html