API 30 crash
See original GitHub issueDescription
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:
- Created 3 years ago
- Reactions:7
- Comments:10 (1 by maintainers)
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.
We found the same issue. As a temporary workaround, we have to run the tests in an emulator with API 29.