Not able to use MockK in Android Espresso UI Testing
Asked Answered
R

2

5

I am getting a error when trying to use MockK in UI test which was perfectly working in Unittest cases

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

When tried to add

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

Observed this error

2 files found with path 'mockito-extensions/org.mockito.plugins.MockMaker'. 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

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)

val presenter = mockk<LoginPresenter>()

val view = mockk<LoginView>()

How to perform a mock api call like this

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

every { presenter.onLoginButtonClicked("[email protected]","Abc123")  } returns  view.onCognitoLoginSuccess()
Roan answered 28/7, 2022 at 2:13 Comment(5)
What versions of mockk, Android, Kotlin are you working with? Can you include some code that reproduces the issue?Coad
@Coad Updated versions in the postRoan
Can you try MockK 1.12.5? If that doesn't work, then can you make an issue? github.com/mockk/mockk/issues/newCoad
Hi i tried MockK 1.12.5 and this error is not reproduced thanks, i have a query updated in postRoan
@Coad how to initialize MockK in espresso UI testingRoan
V
28

For me adding this solved the problem:

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

I found this here. Hope it helps.

Vector answered 17/10, 2022 at 9:21 Comment(2)
I had the same issue with MockK version 1.13.8. This answer solved the problem.Overtone
Same here, with MockK version 1.13.9.Cracy
D
3

Accorfing to here :

Instrumented Android tests are all failing due to issue with mockk 1.12.4

I used io.mockk:mockk-android:1.12.4 and i have same issue..

SOLUTION: I change the version of io.mockk:mockk-android to 1.12 3 and test runed fine for me

androidTestImplementation "io.mockk:mockk-android:1.12.3"
Dannica answered 21/1, 2023 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.