java.lang.NoClassDefFoundError: Could not initialize class androidx.lifecycle.LiveData$Subclass
See original GitHub issuePrerequisites
- I am running the latest version
- I checked the documentation and found no answer
- I checked to make sure that this issue has not already been filed
Expected Behavior
Unit tests that mock LiveData in a Robolectric unit test continue to function.
Current Behavior
What is the current behavior?
Mockk crashes. See referenced stacktrace below.
Failure Information (for bugs)
See issue reported to Robolectric: https://github.com/robolectric/robolectric/issues/5871
Steps to Reproduce
See sample project: https://github.com/zoubarry/TestApp
Context
- MockK version: 1.10.0
- OS: Mac OS 10.15.6
- Kotlin version: 1.3.72
- JDK version: openjdk8
- JUnit version: 4.12
- Type of test: unit test
Stack trace
java.lang.IllegalAccessError: tried to access class androidx.lifecycle.LiveData$ObserverWrapper from class androidx.lifecycle.LiveData$Subclass0
at androidx.lifecycle.LiveData$Subclass0.<clinit>(Unknown Source)
at sun.reflect.GeneratedSerializationConstructorAccessor5.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
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$1.invoke(JvmAnyValueGenerator.kt:27)
at io.mockk.impl.instantiation.AnyValueGenerator.anyValue(AnyValueGenerator.kt:27)
at io.mockk.impl.instantiation.JvmAnyValueGenerator.anyValue(JvmAnyValueGenerator.kt:23)
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:263)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:25)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at com.example.testapp.MainViewModel.getData(MainViewModel.kt:13)
at com.example.testapp.MainActivityTest$mockViewModel$1$1.invoke(MainActivityTest.kt:31)
at com.example.testapp.MainActivityTest$mockViewModel$1$1.invoke(MainActivityTest.kt:24)
at io.mockk.impl.eval.RecordedBlockEvaluator$record$block$1.invoke(RecordedBlockEvaluator.kt:24)
at io.mockk.impl.eval.RecordedBlockEvaluator$enhanceWithNPERethrow$1.invoke(RecordedBlockEvaluator.kt:74)
at io.mockk.impl.recording.JvmAutoHinter.autoHint(JvmAutoHinter.kt:23)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:36)
at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
at io.mockk.MockKDsl.internalEvery(API.kt:92)
at io.mockk.MockKKt.every(MockK.kt:98)
at com.example.testapp.MainActivityTest.<init>(MainActivityTest.kt:31)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:250)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.createTest(RobolectricTestRunner.java:561)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:260)
at org.junit.runners.BlockJUnit4ClassRunner$2.runReflectiveCall(BlockJUnit4ClassRunner.java:309)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:306)
at org.robolectric.internal.SandboxTestRunner$HelperTestRunner.methodBlock(SandboxTestRunner.java:329)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.methodBlock(RobolectricTestRunner.java:570)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:259)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
Minimal reproducible code (the gist of this issue)
@RunWith(AndroidJUnit4::class)
@Config(sdk = [28])
class MainActivityTest {
@get:Rule
val activityRule = ActivityTestRule(MainActivity::class.java, false, false)
private val mockData = MutableLiveData<String>()
private val mockViewModel = spyk(MainViewModel()) {
every { data } returns mockData
every { fetchData() } just Runs
}
@Test
fun `robo livedata errors`() {
activityRule.launchActivity(null)
activityRule.activity.mainViewModel = mockViewModel
mockData.value = "test"
}
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:11
- Comments:17
Top Results From Across the Web
java.lang.NoClassDefFoundError: Could not initialize class XXX
My problem was calling Uri.parse(uriString) as part of a static initializer for a unit test. The Uri class is an Android API, which...
Read more >AndroidX Shadow issues - Google Groups
java.lang.NoClassDefFoundError: Could not initialize class org.robolectric.shadows.ShadowUsageStatsManager. at org.robolectric.
Read more >Kotlin: java.lang.NoClassDefFoundError: Could not ... - YouTube
Kotlin: java. lang. NoClassDefFoundError : Could not initialize class | IntelliJ IDEA Issue Solved. 11K views 1 year ago.
Read more >androidx/fragment/testing/R$style [122321150 ... - Issue Tracker
java.lang.NoClassDefFoundError: androidx/fragment/testing/R$style at androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity.
Read more >java.lang.NoClassDefFoundError: Could not initialize class xxx
Java.lang.NoClassDefFoundError: Could not initialize class xxx Could anyone tell me a possible solution to this exception thrown by my ...
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 Free
Top 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
Happens to me as well with Robolectric 4.4 and MockK 1.10.2.
Edit: Either downgrading to Robolectric 4.3 or running with Java 11 works for me.
Java 9 is not an option. We need a fix for Java 8. This is the latest version compatible:org.robolectric:robolectric:4.3 Updating always ends up with: java.lang.IllegalAccessError: tried to access class androidx.lifecycle.LiveData$ObserverWrapper from class androidx.lifecycle.LiveData$Subclass3