【发布时间】:2019-08-17 12:17:00
【问题描述】:
我正在尝试运行一个简单的仪器测试:
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'
【问题讨论】:
-
我知道这不是一个答案——但对于 kotlin 模拟,我切换到 mockk.io 并且我没有回头。您可以模拟最终类,并且,如果您使用任何异步挂起功能,它会附带内置工具。为 kotlin 内置 kotlin。