java - 单元测试和 UI 测试的模拟 final类

标签 java android kotlin mockito ui-testing

我正在尝试为我的 Android 应用程序编写单元和 espresso UI 测试,使用 kotlin 构建。由于在 kotlin 中,默认情况下每个类都是最终类,因此为了模拟最终类,我使用了支持模拟最终类的mockInline库。 因为我必须编写单元测试和 UI 测试。我通过以下方式添加了库

testImplementation 'org.mockito:mockito-inline:2.13.0'
androidTestImplementation 'org.mockito:mockito-inline:2.13.0'

但是在运行 UI 测试时它会抛出

More than one file was found with OS independent path 'mockito-extensions/org.mockito.plugins.MockMaker'

有没有办法使用mockInline库来模拟单元和UI测试的最终类

最佳答案

我们无法使用官方问题跟踪器here中提到的mockito内联插件来模拟UI测试(Android测试)的最终类。

背后的原因是

"There is no real possibility to make this work in Android at the moment as it lacks the instrumentation API on top of which we are operating. The Android VM is not a standard VM and only implements a subset of the Java specification. As long as Google does not choose to extend its JVM, I am afraid that this feature will not work."

如上所述here

模拟 Android 测试的最终类

  • 我们可以使用DexOpener能够在 Android 中模拟最终类的库。

  • 我们还可以使用Kotlin Open Plugin这使得所有最终类在没有显式 open 关键字的情况下打开。

关于java - 单元测试和 UI 测试的模拟 final类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56485610/

相关文章:

android - 如何找到官方的android studio旧版本

kotlin - 如何将 Kotlin 的 `with` 表达式用于可空类型

java - 如何使用Play框架上传文本文件?

java - 如何在 java 中正确加载 BufferedImage?

android - Dagger 2注入(inject)相同对象类型的多个实例

android - 推特提要 ListView 问题

spring - 如何从Spring Webflux Controller 捕获异常?

java - 如何在android中创建圆角surfaceview

java - 调用一个类给我带来问题(坏类 - 类文件包含错误的类)

java - 在Android中实现类似bucket的方法