我正在尝试运行一个简单的插装测试:
class DefaultTest {
private val networkHelper = Mockito.mock(NetworkHelperImpl::class.java)
@Test fun inter() {
given(networkHelper.isConnectedToNetwork()).willReturn(true)
assertFalse { networkHelper.isConnectedToNetwork(false) }
}
}但我不能因为错误:
Mockito cannot mock/spy because :
- final class我怎么才能避免呢?
正如本指南所说:
https://antonioleiva.com/mockito-2-kotlin/
我正在创建文件:

使用这一行:
mock-maker-inline
但一切都没有改变。
Gradle很糟糕(我正在学习),但它必须工作。我也使用单元测试,所以现在我有:
//Tests
testImplementation 'junit:junit:4.13-beta-3'
testImplementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.41'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.3.41'
androidTestImplementation 'org.mockito:mockito-core:3.0.0'
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"
testImplementation 'org.amshove.kluent:kluent:1.14'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0'发布于 2020-08-04 00:54:18
简单地添加
testImplementation("org.mockito:mockito-inline:2.8.47")已解决问题。
发布于 2019-08-29 15:44:06
请按照以下步骤操作:https://antonioleiva.com/mockito-2-kotlin/
testImplementation.替换androidTestImplementation在我的例子中:"com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0 testImplementation (更新)
testImplementation 'org.mockito:mockito-core:3.0.0‘(更新)
testImplementation 'org.mockito:mockito-android:2.24.5‘(用每个测试类中的testImplementation)
使用Android Studio 3.5进行了测试
发布于 2019-08-17 20:21:54
你试过mockito-kotlin吗?将以下内容添加到您的依赖项中:
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"https://stackoverflow.com/questions/57536196
复制相似问题