Android Kotlin MockMaker问题 [英] Android kotlin mockMaker issues
问题描述
我正在尝试模拟最终课程(因为kotlin中的所有课程默认都是最终课程),并在我的gradle中添加了以下依赖项:
Hi i am trying to mock a final class(as all classes in kotlin are final by default) and added the following dependencies in my gradle:
testImplementation 'junit:junit:4.12'
testImplementation 'au.com.dius:pact-jvm-consumer-junit_2.11:3.5.10'
testImplementation "org.mockito:mockito-android:2.13.0"
testImplementation 'org.mockito:mockito-inline:2.13.0'
testImplementation "org.mockito:mockito-core:2.13.0"
//testImplementation 'io.mockk:mockk:1.8'
testImplementation 'org.assertj:assertj-core:3.8.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation "org.mockito:mockito-core:2.13.0"
androidTestImplementation "org.mockito:mockito-android:2.13.0"
androidTestImplementation 'org.mockito:mockito-inline:2.13.0'
androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2"
mockito-inline应该使您能够模拟最终的kotlin类,因此我使用testImplementation和androidTestImplementation添加到了Java单元测试和仪器测试中
the mockito-inline is supposed to enable you to mock a final kotlin class and so i added to both my java unit test and my instrumental test using testImplementation and androidTestImplementation
在构建项目时,出现以下错误:
On building the project i get the following error:
More than one file was found with OS independent path 'mockito-extensions/org.mockito.plugins.MockMaker'
有什么想法吗?如果我删除了模仿内联的androidTestImplementation,它可以很好地编译,但是当运行仪表测试时,我收到了模仿错误,说它不能模仿最终的类.
Any ideas whats going on? if i remove the androidTestImplementation of the mockitio inline, it compiles fine but when running intrumental test i get the mockito error saying it cannot mock a final class.
推荐答案
为了能够在Kotlin中模拟最终课程,您需要创建一个文件
org.mockito.plugins.MockMaker
(从字面上看)仅包含此行
In order to be able to mock final classes in Kotlin, you’ll need to create a file
org.mockito.plugins.MockMaker
(literally) that contains only this line
mock-maker-inline
并将其放入
test/resources/mockito-extensions
.
有关更多信息,请阅读 https://antonioleiva.com/mockito-2-kotlin/.
For more info please read https://antonioleiva.com/mockito-2-kotlin/.
这篇关于Android Kotlin MockMaker问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!