首页 > 解决方案 > java.lang.ClassCastException:android.os.Looper 无法转换为 org.robolectric.internal.bytecode.ShadowedObject

问题描述

我正在尝试使用带有spoon和falcon-spoon的espresso为我的应用程序自动截屏,android测试应用程序已成功启动并在此之后成功截取了第一个屏幕截图,当我尝试使用onView(withId(buttonId)单击按钮时) .perform(click()) 它抛出以下异常。我试图降级依赖版本并更改 android-sdk 没有结果。

这里抛出的异常。

java.lang.ClassCastException: android.os.Looper cannot be cast to org.robolectric.internal.bytecode.ShadowedObject
at org.robolectric.internal.bytecode.ShadowImpl.extract(ShadowImpl.java:14)
at org.robolectric.shadow.api.Shadow.extract(Shadow.java:25)
at org.robolectric.shadows.ShadowLooper.shadowLooper(ShadowLooper.java:39)
at org.robolectric.shadows.ShadowLooper.shadowMainLooper(ShadowLooper.java:50)
at org.robolectric.android.internal.LocalControlledLooper.drainMainThreadUntilIdle(LocalControlledLooper.java:15)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:99)
at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:43)
at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:94)
at com.dexcom.app.utils.CommonViewsHelperKt.clickNextButton(CommonViewsHelper.kt:43)
at com.dexcom.app.screen_crawler.SetupWizardScreenshots.setupWizardScreenShots(SetupWizardScreenshots.kt:49)
at java.lang.reflect.Method.invoke(Native Method)
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 androidx.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:130)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2218)

这是我用于勺子和浓缩咖啡的 gradle 依赖项:

dependencies {

androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"
androidTestImplementation "androidx.test:rules:1.3.0"
androidTestImplementation "androidx.test:runner:1.3.0"
androidTestImplementation "com.squareup.spoon:spoon-client:2.0.0-SNAPSHOT"
androidTestImplementation "com.jraska:falcon:2.1.1"
androidTestImplementation "com.jraska:falcon-spoon-compat:2.1.1"
}

标签: androidandroid-espressorobolectricandroid-looper

解决方案


我通过从我的主要源集中删除 Robolectric 库依赖项解决了这个问题。我已经将它包含在内以使实用程序工作,但我记得读过如果 Robolectric 测试运行器可用,它将被使用,因为如果它作为依赖项可用,它会自动注册。

所以我删除了这个

implementation "org.robolectric:robolectric:$ROBOLECTRIC_VERSION"

并由自定义实用程序重构。这里的教训是仅将 Robolectric 作为您的单元测试源集的依赖项,即仅使用

testImplementation "org.robolectric:robolectric:$ROBOLECTRIC_VERSION"

推荐阅读