Espresso无法启动其他活动

时间:2018-05-18 11:21:27

标签: android unit-testing android-activity android-espresso ui-testing

Espresso无法找到第二项活动。我正在尝试使用Espresso调用一个新活动的简单测试,但它始终失败并显示下面的错误日志。我该怎么办呢?

这是测试类

public class LoginActivityTest {

    @Rule
    public ActivityTestRule<LoginActivity> mActivityTestRule = new ActivityTestRule<LoginActivity>(LoginActivity.class);

    private LoginActivity loginActivity = null;
    Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(SecondActivity.class.getName(), null, false);

    @Before
    public void setUp() throws Exception {
        loginActivity = mActivityTestRule.getActivity();
    }

    @Test
    public  void writeTestCase(){
        Espresso.onView(withId(R.id.email_sign_in_button)).perform(click());

        Activity secondActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5000);
        assertNotNull(secondActivity);

        Espresso.onView(withId(R.id.email_sign_in_button)).check(matches(withId(R.id.first_number)));

    @After
    public void tearDown() throws Exception {
        loginActivity = null;
    }

}

这是错误日志

java.lang.RuntimeException: No activities found. Did you t to launch the activity by calling getActivity() or startActivitySync or similar?
at android.support.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:169)
at android.support.test.espresso.base.RootViewPicker.get(RootViewPicker.java:83)
at android.support.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:77)
at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.proxyProvideRootView(ViewInteractionModule_ProvideRootViewFactory.java:34)
at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:24)
at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:10)
at android.support.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:62)
at android.support.test.espresso.ViewInteraction$2.call(ViewInteraction.java:273)
at android.support.test.espresso.ViewInteraction$2.call(ViewInteraction.java:265)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

Tests ran to completion.

0 个答案:

没有答案