Reputation: 978
I'm trying to run a simple unit test using Roboelectric however on trying to build an Activity , I'm getting the following error :
java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.
at org.robolectric.android.internal.AndroidTestEnvironment.checkStateAfterTestFailure(AndroidTestEnvironment.java:502)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:581)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:263)
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)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchFieldError: tag_on_apply_window_listener
at androidx.core.view.ViewCompat$Api21Impl.setOnApplyWindowInsetsListener(ViewCompat.java:4733)
at androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener(ViewCompat.java:2510)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:938)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:693)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:303)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
at com.vedantu.app.nativemodules.vQuiz.view.ui.MainQuizActivity.onCreate(MainQuizActivity.kt:31)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:711)
at org.robolectric.android.controller.ActivityController.lambda$create$0(ActivityController.java:106)
at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:195)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:106)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:111)
at org.robolectric.android.fakes.RoboMonitoringInstrumentation.startActivitySyncInternal(RoboMonitoringInstrumentation.java:66)
at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:40)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:195)
at com.vedantu.app.nativemmodules.vQuiz.utils.ReferralUtilTest.testQuizHomeActivity(ReferralUtilTest.kt:109)
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.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
... 6 more
Culprit seems to be this line : java.lang.NoSuchFieldError: tag_on_apply_window_listener
My test file is pretty straighforward :
@Test
fun testQuizHomeActivity(){
val context: Context = RuntimeEnvironment.application.getApplicationContext()
FirebaseApp.initializeApp(context)
val intent: Intent = Intent()
intent.putExtra("userId", "")
intent.putExtra("userName", "userName")
intent.putExtra("upcomingVQuiz", "upcomingVQuiz")
intent.putExtra("source", "source")
shadowOf(getMainLooper()).idle();
val quizHomeActivity = Robolectric.buildActivity(MainQuizActivity::class.java, intent).create().get()
Assert.assertNotNull(activity)
}
The other tests are working without an issue.
Upvotes: 0
Views: 878
Reputation: 11
Are you using gradle setting like
testImplementation "androidx.core:core-ktx:+" ?
If so, try using fixed verision like 1.3.2. I can solve same issue by that. It seems ViewCompat's implementation has been changed.
Upvotes: 1