I want to use Mockito for unit testing, so I added the Mockito library into my gradle dependencies.
testImplementation 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.12.0'
But still, I can not use any Mockito annotations.
/androidTest/ExampleTest.kt
@RunWith(MockitoJUnitRunner::class) // Unresolved reference MockitoJUnitRunner
@Mock // Unresolved reference Mock
What I'm missing it?
Mockito has been around since the early days of Android development and eventually became the de-facto mocking library for writing unit tests. Mockito and Mockk are written in Java and Kotlin, respectively, and since Kotlin and Java are interoperable, they can exist within the same project.
Conclusion. Mockito is used to test final classes in Kotlin. Mockito is a widely used framework for mobile developers to write unit tests.
In Kotlin, all classes and methods are final. While this helps us write immutable code, it also causes some problems during testing. Most JVM mock libraries have problems with mocking or stubbing final classes. Of course, we can add the “open” keyword to classes and methods that we want to mock.
You need to add the following dependencies in your app's build.gradle:
dependencies {
// ... more entries
testCompile 'junit:junit:4.12'
// required if you want to use Mockito for unit tests
testImplementation 'org.mockito:mockito-core:2.24.5'
// required if you want to use Mockito for Android tests
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
}
And click on sync
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With