为什么在Espresso / Robolectric android测试中单击RecyclerView项目时无法启动活动?

时间:2019-06-19 07:58:27

标签: java android android-recyclerview android-espresso robolectric

我正在用Android Espresso编写测试,在回收者视图中单击项目后,应该打开项目详细信息(开始新活动)。 在设备上一切正常。如果我尝试使用Robolectric进行此测试,则第二个活动将不会开始。

FirstActivity使用以下方法创建:

@Rule
public ActivityScenarioRule<FirstActivity> activityRule = new ActivityScenarioRule<>(FirstActivity.class);

点击监听器后,第二个活动开始:

activity.startActivity(new Intent(activity, SecondActivity.class).putExtra(PARAM_ID, paramId));

当我尝试通过单击RecyclerView中的项目来启动第二个活动时,出现问题:

onView(withId(R.id.recyclerview_messages)).check(matches(isDisplayed())).perform(RecyclerViewActions.actionOnItemAtPosition(idx, click()));

尽管此行没有返回任何错误,请检查活动是否以错误结束:

onView(withId(R.id.relativelayout_messages_detail)).check(matches(isDisplayed()))

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: my.project.app.test:id/relativelayout_messages_detail
at java.lang.Thread.getStackTrace(Thread.java:1559)
    at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
    at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
    at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:322)
    at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:306)
    at my.project.app.BaseActivityTest.checkAndGetView(BaseActivityTest.java:72)
    at my.project.app.FirstActivityTest.testMessageReadCount(FirstActivityTest.java:127)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:546)
    at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:252)
    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)

使用:
Robolectric:4.3
浓咖啡:3.2.0
androidx.test:1.2.0
Android SDK:24
Java:8

有人可以指出正确的方向吗,可能是问题所在? 谢谢。

0 个答案:

没有答案