如何为Kotlin和Android设置Mockito

Exp*_* be 5 android mockito kotlin

我想使用Mockito进行单元测试,所以我将Mockito库添加到我的gradle依赖项中.

testImplementation 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.12.0' 
Run Code Online (Sandbox Code Playgroud)

但是,我仍然不能使用任何Mockito注释.

/androidTest/ExampleTest.kt

@RunWith(MockitoJUnitRunner::class) // Unresolved reference MockitoJUnitRunner
@Mock // Unresolved reference Mock
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Shy*_*dda 16

您应该在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'
}
Run Code Online (Sandbox Code Playgroud)

然后点击同步