unit-testing - 由于 mockito,Android 仪器测试失败
问题描述
嗨,我收到以下错误
org.koin.error.BeanInstanceCreationException: Can't create definition for 'Single [name='NetworkControllerContract',class='com.network.contract.NetworkControllerContract']' due to error :
Mockito cannot mock this class: class com.network.NetworkController.
我已经手动打开了这些课程..
open class NetworkController constructor(private val networkHelper: NetworkHelperContract) : NetworkControllerContract{.....}
open interface NetworkControllerContract {
}
//my test class
@RunWith(AndroidJUnit4::class)
class MyUnitTest: KoinTest {
single<NetworkControllerContract> {
Mockito.mock(NetworkController::class.java) //where it crashes
}
val networkController: NetworkControllerContract by inject()
}
我使用的依赖项
def mockito = "2.21.0"
//android instrumental test
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.ext:truth:1.1.0'
androidTestImplementation 'androidx.test:core:1.1.0'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation('androidx.arch.core:core-testing:2.0.0') {
exclude group: 'org.mockito:mockito-core'
}
androidTestUtil 'androidx.test:orchestrator:1.1.1'
androidTestImplementation 'com.github.tmurakami:dexopener:2.0.0'
androidTestImplementation 'com.jraska.livedata:testing-ktx:0.6.0'
androidTestImplementation 'com.jraska.livedata:testing:0.6.0'
androidTestImplementation "org.mockito:mockito-core:$mockito"
androidTestImplementation("org.mockito:mockito-android:$mockito") {
exclude group: 'org.mockito'
}
androidTestImplementation('org.koin:koin-test:1.0.2') {
exclude group: 'org.mockito'
}
testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:$mockito"
testImplementation "org.mockito:mockito-android:$mockito"
testImplementation "org.mockito:mockito-inline:$mockito"
testImplementation 'org.koin:koin-test:1.0.2'
testImplementation 'androidx.test.ext:junit:1.1.0'
testImplementation 'androidx.arch.core:core-testing:2.0.0'
testImplementation 'com.jraska.livedata:testing-ktx:0.6.0'
testImplementation 'com.jraska.livedata:testing:0.6.0'
testImplementation 'androidx.test.ext:truth:1.1.0'
解决方案
问题出在您的设置中:
testImplementation "org.mockito:mockito-android:$mockito"
此依赖项使用 Android 内部,在运行单元测试时不可用。
推荐阅读
- python - 在 python 中打印菜单
- mongodb - 如何在 MongoDB(大数据)中导出较小的集合?聚合超时!(非常感谢任何大数据帮助!)
- django - 当前路径在 Django 中不匹配
- sql - SQLSTATE [22007]:日期时间格式无效:1292 日期值不正确:laravel 中“日期”列的“1399/06/31”
- android - 如何在Android中检测通话状态
- c - 编译基本 OptionMenu 示例时分配给“Widget”的不兼容整数到指针转换
- python - 将行转换为数据框python中的列
- android - flutter firebase如何返回此数据?
- django - 使用 Azure CLI 部署 Django 应用程序时,是否可以使用 requirements.txt 文件?
- nearprotocol - 在近cli附加存款