无法在使用 Mockito 和 Kotlin 的测试中模拟最终课程 [英] Can't mock final class in test using Mockito with Kotlin
本文介绍了无法在使用 Mockito 和 Kotlin 的测试中模拟最终课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试运行一个简单的仪器测试:
类 DefaultTest {私有 val networkHelper = Mockito.mock(NetworkHelperImpl::class.java)@Test 有趣的 inter() {给定(networkHelper.isConnectedToNetwork()).willReturn(true)assertFalse { networkHelper.isConnectedToNetwork(false) }}}
但我不能因为错误:
Mockito 无法模拟/窥探,因为:- 最后一堂课
如何避免?
正如本指南所说:
用这一行:
mock-maker-inline
但没有任何改变.
Gradle 很糟糕(我正在学习),但它必须有效.我也使用单元测试,所以现在我有:
//测试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'
解决方案
简单添加
testImplementation("org.mockito:mockito-inline:2.8.47")
解决了这个问题.
I'm trying to run a simple instrumentation test:
class DefaultTest {
private val networkHelper = Mockito.mock(NetworkHelperImpl::class.java)
@Test fun inter() {
given(networkHelper.isConnectedToNetwork()).willReturn(true)
assertFalse { networkHelper.isConnectedToNetwork(false) }
}
}
But i cant bacause of error:
Mockito cannot mock/spy because :
- final class
How can i avoid it?
As this guide says:
https://antonioleiva.com/mockito-2-kotlin/
I'm create file:
With this line:
mock-maker-inline
But nothing changes.
Gradle is bad(i'm learning), but it must work. I use unit test too, so now i have:
//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'
解决方案
Simply adding
testImplementation("org.mockito:mockito-inline:2.8.47")
resolved the issue.
这篇关于无法在使用 Mockito 和 Kotlin 的测试中模拟最终课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文