kotlin 无法在Android Espresso UI测试中使用MockK

ecr0jaav  于 2023-01-26  发布在  Kotlin
关注(0)|答案(2)|浏览(238)

我在UI测试中尝试使用MockK时遇到错误,而MockK在单元测试用例中运行良好

MockK could not self-attach a jvmti agent to the current VM

完整错误报告

Caused by: io.mockk.proxy.MockKAgentException: MockK could not self-attach a jvmti agent to the current VM. This feature is required for inline mocking.
This error occured due to an I/O error during the creation of this agent: java.io.IOException: Unable to dlopen libmockkjvmtiagent.so: dlopen failed: library "libmockkjvmtiagent.so" not found

Potentially, the current VM does not support the jvmti API correctly
at io.mockk.proxy.android.AndroidMockKAgentFactory.init(AndroidMockKAgentFactory.kt:67)
at io.mockk.impl.JvmMockKGateway.<init>(JvmMockKGateway.kt:46)
at io.mockk.impl.JvmMockKGateway.<clinit>(JvmMockKGateway.kt:186)
... 30 more
Caused by: java.io.IOException: Unable to dlopen libmockkjvmtiagent.so: dlopen failed: library "libmockkjvmtiagent.so" not found
at dalvik.system.VMDebug.nativeAttachAgent(Native Method)
at dalvik.system.VMDebug.attachAgent(VMDebug.java:693)
at android.os.Debug.attachJvmtiAgent(Debug.java:2617)
at io.mockk.proxy.android.JvmtiAgent.<init>(JvmtiAgent.kt:48)
at io.mockk.proxy.android.AndroidMockKAgentFactory.init(AndroidMockKAgentFactory.kt:40)

让我知道是否有任何其他方法来初始化MockK,使在Espresso中使用
尝试添加时

androidTestImplementation "org.mockito:mockito-inline:$mockitoVersion"

观察到此错误
找到2个路径为“mockito-extensions/org.mockito.plugins. MockMaker”的文件。添加packagingOptions块可能会有所帮助,请参阅https://developer.android.com/reference/tools/gradle-api/7.2/com/android/build/api/dsl/ResourcesPackagingOptions了解详细信息

版本

mockk version = 1.12.4
Android = 32
kotlin_version = '1.6.21'

添加到android UI测试用例(Espresso)时导致此问题的代码

val presenter = mockk<LoginPresenter>()

val view = mockk<LoginView>()

如何执行这样的模拟API调用

val presenter = mockk<LoginPresenter>()
    val view = mockk<LoginView>()

onView(withId(R.id.button_login)).perform(loginClick())

但我希望在**perform()**中调用模拟API,而不是loginClick(),我能否调用下面的执行,以便我的应用使用模拟API,或者是否有任何方法使我的整个测试用例文件使用模拟数据

every { presenter.onLoginButtonClicked("bc@mail.com","Abc123")  } returns  view.onCognitoLoginSuccess()
zqdjd7g9

zqdjd7g91#

对我来说,添加这个解决了问题:

android {
  testOptions {
    packagingOptions {
      jniLibs {
        useLegacyPackaging = true
      }
    }
  }
}

我在这里找到这个。希望能帮上忙。

jogvjijk

jogvjijk2#

根据here
由于mockk1.12.4问题,仪器化Android测试全部失败
我用的是io.mockk:mockk-android:1.12.4,也有同样的问题。

解决方案:我将io.mockk:mockk-android的版本更改为1.12 3,测试运行良好

androidTestImplementation "io.mockk:mockk-android:1.12.3"

相关问题