如何为Kotlin和Android设置Mockito

我想使用Mockito进行unit testing,所以我将Mockito库添加到我的gradle依赖项中。

testImplementation 'junit:junit:4.12' testCompile 'org.mockito:mockito-core:2.12.0' 

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

/androidTest/ExampleTest.kt

 @RunWith(MockitoJUnitRunner::class) // Unresolved reference MockitoJUnitRunner @Mock // Unresolved reference Mock 

我错过了什么?

您应该在build.gradle中使用以下代码:

 dependencies { // ... more entries testCompile 'junit:junit:4.12' // required if you want to use Mockito for unit tests testCompile 'org.mockito:mockito-core:2.7.22' // required if you want to use Mockito for Android tests androidTestCompile 'org.mockito:mockito-android:2.7.22' } 

然后点击同步

您可能需要另一个依赖项:

 androidTestCompile 'org.mockito:mockito-android:2.12.0' 

或者,您可以尝试手动导入注释:

 import static org.mockito.Mockito.*; 

它可能是它没有正确导入,这就是为什么它显示为一个未解决的参考。 自动import有其缺陷