在尝试使用MockK进行UI测试时,我遇到了错误,而在单元测试中它是完美工作的。
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"
我发现了这个错误。
找到了两个路径为'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()中调用 mock api 而不是 loginClick(),是否可以通过以下方式调用执行以便我的应用程序使用 mock api,或者有没有办法使我的整个测试用例文件使用 mock 数据。
every { presenter.onLoginButtonClicked("bc@mail.com","Abc123") } returns view.onCognitoLoginSuccess()