简体   繁体   English

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

[英]Not able to use MockK in Android Espresso UI Testing

I am getting a error when trying to use MockK in UI test which was perfectly working in Unittest cases尝试在 UI 测试中使用 MockK 时出现错误,它在 Unittest 案例中完美运行

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

Full error report完整的错误报告

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)

Let me know is there any other way to initialize the MockK to make use in Espresso让我知道是否有任何其他方法可以初始化 MockK 以在 Espresso 中使用

When tried to add当试图添加

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

Observed this error观察到这个错误

2 files found with path 'mockito-extensions/org.mockito.plugins.MockMaker'.找到路径为“mockito-extensions/org.mockito.plugins.MockMaker”的 2 个文件。 Adding a packagingOptions block may help, please refer to https://developer.android.com/reference/tools/gradle-api/7.2/com/android/build/api/dsl/ResourcesPackagingOptions for more information添加 packagingOptions 块可能会有所帮助,请参阅https://developer.android.com/reference/tools/gradle-api/7.2/com/android/build/api/dsl/ResourcesPackagingOptions了解更多信息

Versions版本

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

Code which causes this issue when added in android UI testcases(Espresso)在 android UI 测试用例(Espresso)中添加时导致此问题的代码

val presenter = mockk<LoginPresenter>()

val view = mockk<LoginView>()

How to perform a mock api call like this如何执行这样的模拟 api 调用

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


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

But i want mock api to be called instead of loginClick() in perform() can i call some how the below execution so that my app uses mock api's or is there any way to make my entire testcase file use mockk data但是我想在perform()中调用模拟 api 而不是 loginClick() 我可以调用一些下面的执行方式以便我的应用程序使用模拟 api 或者有什么方法可以使我的整个测试用例文件使用 mockk 数据

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

For me adding this solved the problem:对我来说,添加这个解决了问题:

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

I found this here .我在这里找到了这个。 Hope it helps.希望能帮助到你。

Accorfing to here :根据这里

Instrumented Android tests are all failing due to issue with mockk 1.12.4由于 mockk 1.12.4的问题,已检测的 Android 测试全部失败

I used io.mockk:mockk-android:1.12.4 and i have same issue..我使用io.mockk:mockk-android:1.12.4我有同样的问题..

SOLUTION: I change the version of io.mockk:mockk-android to 1.12 3 and test runed fine for me解决方案:我将 io.mockk:mockk-android 的版本更改为1.12 3并测试运行正常

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM