question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Description

When using spyk() in a test using API 30, it crashes.

Prerequisites

  • implementation “io.mockk:mockk-android:1.10.2”
  • emulator: Pixel 3 API 30 *this is important, because the crash did not reproduce on an emulator with API 29.

Steps

Start a new Android Studio project (Android Studio Version: 4.0.1)

Make a MyUtil class:

    class MyUtil {
        fun sayHi(): String {
            return "Hi"
        }
    }

Make a test in androidTest/

    @RunWith(AndroidJUnit4::class)
    class ExampleInstrumentedTest {
        @Test
        fun testMockMyUtil() {
            val mockMyUtil = spyk(MyUtil())
            every { mockMyUtil.sayHi() } returns "MOCK HI"
        }
    }

Run the test

Expected: Test succeeds

Actual: Test fails with this stacktrace:

java.lang.ExceptionInInitializerError at com.example.pg_mockk.ExampleInstrumentedTest.mockMyUtil(ExampleInstrumentedTest.kt:31) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:154) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2205) Caused by: io.mockk.proxy.MockKAgentException: Could not set up hiddenApiExemptions at io.mockk.proxy.android.AndroidMockKAgentFactory.init(AndroidMockKAgentFactory.kt:105) at io.mockk.impl.JvmMockKGateway.<init>(JvmMockKGateway.kt:46) at io.mockk.impl.JvmMockKGateway.<clinit>(JvmMockKGateway.kt:172) … 29 more

Link to Github example that crashes

https://github.com/TroyEvans1010/PG_Mockk.git

Other info

Here is the app-level build.gradle (It’s what you get when you start a new project in Android Studio and add Mockk):

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.pg_mockk"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    // Mockk
    androidTestImplementation "io.mockk:mockk-android:1.10.2"
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
tkmnzmcommented, Jan 22, 2021

Version 1.10.0 is available as a workaround. In my case, 1.10.0 worked on API 30.

The crash occurs in the following lines. https://github.com/mockk/mockk/blob/58c3d5b85a8b0441619733251c74c4b1da38703d/agent/android/src/main/kotlin/io/mockk/proxy/android/AndroidMockKAgentFactory.kt#L105

This try-catch code block is to fix #351. But mocking hidden method is not necessary for some products.

I think it would be better to fix this code block so that it doesn’t run on API 30.

1reaction
edisonspencercommented, Jan 6, 2021

We found the same issue. As a temporary workaround, we have to run the tests in an emulator with API 29.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Difference between API 30 and API 31 Android - Stack Overflow
But it crashed in my other one phone with Android 12. Now I'm confused like "The hell is going on". Here's my Java...
Read more >
Crashes - Android Developers
An Android app crashes whenever there's an unexpected exit caused by an ... For example, the Google Home app saw a 30% reduction...
Read more >
Get Android NDK crash reports | Firebase Crashlytics - Google
This guide describes how to configure crash reporting with the Firebase Crashlytics SDK for NDK. If you're looking for how to get started...
Read more >
Bug - Android crash when seting value in VideoPlayer.frame
Bug Android crash when seting value in VideoPlayer.frame ... No crash when Target API level 30 -- Crash on API level 31 and...
Read more >
Android Question [ SOLVED] Target SDK 31 widget crash - B4X
But does it work targeting SDK 30? If it does it looks, from the error, that Erel may need to adjust some compilation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found