Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gekylafas opened this issue Nov 18, 2021 · 17 comments · Fixed by #782

Comments

@gekylafas
Copy link

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 62d5c79.

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)

@gekylafas
Copy link
Author

Any updates on this, anyone?

@gekylafas
Copy link
Author

This still happens in 1.12.2

@jairrab
Copy link

jairrab commented Jan 17, 2022

Also running into the same issue on 1.12.1 and 1.12.2

@kingargyle
Copy link

This issue resurface, and Robolectric has some findings, robolectric/robolectric#5871

@kingargyle
Copy link

Unfortunately they are saying the Mockk will probably need to fix it... we are tossing the hot potatoe again.

@hoisie
Copy link
Contributor

hoisie commented Jan 24, 2022

I did a git bisect and this is the commit that introduced the regression: 62d5c79

@gekylafas
Copy link
Author

Yeap, exactly the commit I found when git bisecting.
Unfortunately, no response so far from mockk maintainer(s).

@Raibaz
Copy link
Collaborator

Raibaz commented Jan 24, 2022

Thanks for bisecting and investigating this.

Since the commit you mention was introduced to fix #280 and the comments in that issue mention a workaround, I may consider reverting it.

I'd be more than happy to review a PR with it, actually.

@hoisie
Copy link
Contributor

hoisie commented Jan 24, 2022

I put up #782 which fixes it from the Robolectric side.

@Raibaz
Copy link
Collaborator

Raibaz commented Jan 24, 2022

Awesome, thanks a lot!

hoisie added a commit to hoisie/mockk that referenced this issue Jan 24, 2022
This fixes an issue using MockK on classes defined in a Robolectric
class loader.

Fixes mockk#754
Partially fixes robolectric/robolectric#5871
@hoisie
Copy link
Contributor

hoisie commented Jan 25, 2022

@Raibaz I would be interested in capturing this issue, and perhaps a couple of other basic Robolectric + MockK integrations, in a test case. Would a new directory such as client-tests/robolectric be appropriate?

@Raibaz
Copy link
Collaborator

Raibaz commented Jan 25, 2022

Sure, that'd be perfect, and thanks a lot for doing this!

@kingargyle
Copy link

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

@ILikeYourHat
Copy link

@Raibaz When this fix will be released? We cannot downgrade MockK to 1.12.0 due to some other bug that was fixed in 1.12.1 😢

@Raibaz
Copy link
Collaborator

Raibaz commented Feb 8, 2022

I'm planning on putting a release out sometime next week, sorry but this week's a pretty busy one :(

@ILikeYourHat
Copy link

@Raibaz bump 🙂

@Raibaz
Copy link
Collaborator

Raibaz commented Feb 28, 2022

Sorry about this, v1.12.3 just came out :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants