Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up Mockito for Kotlin and Android

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?

like image 985
Expert wanna be Avatar asked Nov 14 '17 09:11

Expert wanna be


People also ask

Can we use Mockito with Kotlin?

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.

What is Mockito Android?

Conclusion. Mockito is used to test final classes in Kotlin. Mockito is a widely used framework for mobile developers to write unit tests.

What is mock Kotlin?

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.


Video Answer


1 Answers

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

like image 169
Shailendra Madda Avatar answered Sep 28 '22 05:09

Shailendra Madda