"failed to access class androidx.lifecycle.LiveData$ObserverWrapper" error in 1.12.1 when stubbing LiveData in test with AndroidJUnit4 runner
See original GitHub issueAfter upgrading from 1.12.0 to 1.12.1 the following sample test fails:
package io.mockk.test.livedata
import androidx.lifecycle.LiveData
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.mockk.every
import io.mockk.mockk
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class MockkLiveDataTest {
interface VM {
val liveData: LiveData<String>
}
@Test
fun `liveData is stubbed successfully`() {
val vm = mockk<VM>()
every { vm.liveData } returns mockk()
}
}
I managed to git bisect
the problem and the first bad commit was 62d5c79b68c2bcf6d9af0229f74ac2bb00a85710.
Error output
failed to access class androidx.lifecycle.LiveData$ObserverWrapper from class androidx.lifecycle.LiveData$Subclass1 (androidx.lifecycle.LiveData$ObserverWrapper is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @55a304e4; androidx.lifecycle.LiveData$Subclass1 is in unnamed module of loader net.bytebuddy.dynamic.loading.MultipleParentClassLoader @7cc4c5c3)
java.lang.IllegalAccessError: failed to access class androidx.lifecycle.LiveData$ObserverWrapper from class androidx.lifecycle.LiveData$Subclass1 (androidx.lifecycle.LiveData$ObserverWrapper is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @55a304e4; androidx.lifecycle.LiveData$Subclass1 is in unnamed module of loader net.bytebuddy.dynamic.loading.MultipleParentClassLoader @7cc4c5c3)
at androidx.lifecycle.LiveData$Subclass1.<clinit>(Unknown Source)
at jdk.internal.reflect.GeneratedSerializationConstructorAccessor9.newInstance(Unknown Source)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
at io.mockk.proxy.jvm.ObjenesisInstantiator.instanceViaObjenesis(ObjenesisInstantiator.kt:75)
at io.mockk.proxy.jvm.ObjenesisInstantiator.instantiateViaProxy(ObjenesisInstantiator.kt:66)
at io.mockk.proxy.jvm.ObjenesisInstantiator.instance(ObjenesisInstantiator.kt:29)
at io.mockk.proxy.jvm.ProxyMaker.instantiate(ProxyMaker.kt:75)
at io.mockk.proxy.jvm.ProxyMaker.proxy(ProxyMaker.kt:42)
at io.mockk.impl.instantiation.JvmMockFactory.newProxy(JvmMockFactory.kt:34)
at io.mockk.impl.instantiation.AbstractMockFactory.newProxy$default(AbstractMockFactory.kt:29)
at io.mockk.impl.instantiation.AbstractMockFactory.temporaryMock(AbstractMockFactory.kt:127)
at io.mockk.impl.recording.states.RecordingState$call$retValue$1.invoke(RecordingState.kt:72)
at io.mockk.impl.instantiation.JvmAnyValueGenerator$anyValue$2.invoke(JvmAnyValueGenerator.kt:35)
at io.mockk.impl.instantiation.AnyValueGenerator.anyValue(AnyValueGenerator.kt:34)
at io.mockk.impl.instantiation.JvmAnyValueGenerator.anyValue(JvmAnyValueGenerator.kt:31)
at io.mockk.impl.recording.states.RecordingState.call(RecordingState.kt:70)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:266)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:23)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:21)
at io.mockk.proxy.jvm.advice.BaseAdvice.handle(BaseAdvice.kt:42)
at io.mockk.proxy.jvm.advice.jvm.JvmMockKProxyInterceptor.interceptNoSuper(JvmMockKProxyInterceptor.java:45)
...
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:591)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:274)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:88)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:17
- Comments:17 (3 by maintainers)
Top Results From Across the Web
Unit test fails for Idle and Loading states - Stack Overflow
So this line will be passed as expected in local unit test : verify(resource, times(3)).onChanged(captor.capture()).
Read more >Unit-testing LiveData and other common observability problems
Let's write a simple test for it: This test fails because LiveData doesn't do more work than needed. Reading liveData2.value ...
Read more >Testing Android Architecture Components - RayWenderlich.com
In this tutorial, you'll use the following classes to manage your UI component lifecycle and handle data persistence: LiveData helps you build ...
Read more >Advanced Android in Kotlin 05.1: Testing Basics
This first codelab covers the basics of testing on Android, you'll write your first tests and learn how to test LiveData and ViewModel...
Read more >Building Unit Tests for ViewModel in TDD style - ProAndroidDev
You will learn simple way of creating unit tests for Android ViewModel implementation with step-by-step explanation and ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Raibaz is there a timeline for a release with this fix?
I’m planning on putting a release out sometime next week, sorry but this week’s a pretty busy one 😦