Espresso:如何测试开始新活动?

时间:2018-01-28 17:44:46

标签: android-espresso

如果我有MainActivity和DetailActivity,我该如何测试DetailActivity是否已经启动?

public class MainScreenTest {

    @Rule
    public IntentsTestRule<MainActivity> mainActivityRule =
            new IntentsTestRule<>(MainActivity.class);

    @Test
    public void shouldOpenDetailActivityOnClick() {
        onView(withId(R.id.button)).check(matches(withText("Detail")));

        onView(withId(R.id.button))
                .perform(click());

        ...
    }

}

1 个答案:

答案 0 :(得分:0)

这是一种方法:

public class MainScreenTest {

    @Rule
    public IntentsTestRule<MainActivity> mainActivityRule =
            new IntentsTestRule<>(MainActivity.class);

    @Test
    public void shouldOpenDetailActivityOnClick() {
        onView(withId(R.id.button)).check(matches(withText("Detail")));

        onView(withId(R.id.button))
                .perform(click());

        intended(hasComponent("com.example.androidplayground.DetailActivity"));
    }

}