Android Kotlin MockMaker问题

jon*_*ney 6 android mockito kotlin

嗨,我正在尝试模拟最后一个类(默认情况下,kotlin中的所有类都是最终类),并在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"
Run Code Online (Sandbox Code Playgroud)

该模仿内联应该使您能够模拟最终的kotlin类,因此我使用testImplementation和androidTestImplementation将其添加到我的Java单元测试和仪器测试中

在构建项目时,出现以下错误:

More than one file was found with OS independent path 'mockito-extensions/org.mockito.plugins.MockMaker'
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?如果我删除了类内联的androidTestImplementation,它可以正常编译,但是在运行仪器测试时,我收到了类比错误,说它无法模拟最终类。

Dem*_*god 7

为了能够在Kotlin中模拟最终类,您需要创建一个文件org.mockito.plugins.MockMaker(从字面上看),该文件 仅包含此行

mock-maker-inline
Run Code Online (Sandbox Code Playgroud)

并放入 test/resources/mockito-extensions

有关更多信息,请阅读https://antonioleiva.com/mockito-2-kotlin/

  • 我认为这不再是真的。这就是内联依赖的重点。另外,我很确定我们还不能在androidTest源集中模拟最终类。Android SDK的一些限制。 (2认同)