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.

"failed to access class androidx.lifecycle.LiveData$ObserverWrapper" error in 1.12.1 when stubbing LiveData in test with AndroidJUnit4 runner

See original GitHub issue

After 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:closed
  • Created 2 years ago
  • Reactions:17
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
kingargylecommented, Jan 27, 2022

@Raibaz is there a timeline for a release with this fix?

3reactions
Raibazcommented, Feb 8, 2022

I’m planning on putting a release out sometime next week, sorry but this week’s a pretty busy one 😦

Read more comments on GitHub >

github_iconTop 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 >

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