我正在创建文件: 此行: mock-maker-inline 但是什么都没有改变. Gradle不好(我正在学习),但必须有效.我也使用单元测试,所以现在有了://TeststestImplementation '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'解决方案 执行以下步骤: https://antonioleiva.com/mockito-2-kotlin/ 更新库并将androidTestImplementation替换为 testImplementation .就我而言: testImplementation"com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0(更新) testImplementation'org.mockito:mockito-core:3.0.0'(更新) testImplementation'org.mockito:mockito-android:2.24.5'(用testImplementation代替androidTestImplementation) 从每个测试类中删除 MockitoAnnotations.initMocks(this). 已通过Android Studio 3.5测试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 classHow can i avoid it?As this guide says:https://antonioleiva.com/mockito-2-kotlin/I'm create file:With this line:mock-maker-inlineBut nothing changes.Gradle is bad(i'm learning), but it must work. I use unit test too, so now i have://TeststestImplementation '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' 解决方案 Follow this steps: https://antonioleiva.com/mockito-2-kotlin/Update the libraries and replace androidTestImplementation with testImplementation. In my case:testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0 (update)testImplementation 'org.mockito:mockito-core:3.0.0' (update)testImplementation 'org.mockito:mockito-android:2.24.5' (replace androidTestImplementation with testImplementation)Remove MockitoAnnotations.initMocks(this) from each Test class.Tested with Android Studio 3.5 这篇关于无法在Mockito和Kotlin中模拟最后的测试课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 01:56