如何使用ActivityInstrumentationTestCase2阻止在每次测试期间启动活动

时间:2016-05-09 04:48:09

标签: android unit-testing testing

我有一个打算测试应用程序UI的类,但是,每次使用@Test注释的新方法启动活动再次启动时 - 我想启动它一次并执行测试推出实例。有没有办法做到这一点?

测试类示例:

@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestActivityHomepage extends ActivityInstrumentationTestCase2 <ActivityHomepage> {

    private static final String TAG = "TestActivityHomepage";


    private ActivityHomepage mActivity;
    public ActivityTestRule<ActivityHomepage> rule = new ActivityTestRule(ActivityHomepage.class, true, false);

    public TestActivityHomepage() {
        super(ActivityHomepage.class);
    }

   @Before
    public void setUp() throws Exception {
        super.setUp();
        Intent intent = new Intent();
        mActivity = rule.launchActivity(intent);
    }

    @Test
    public void testPreconditions() {
        assertNotNull(mActivity);
    }

    @Test
    public void testLoadMoreCharacters () throws InterruptedException {
        RecyclerView characters = (RecyclerView) this.mActivity.findViewById(R.id.characters);
        int previousSize = characters.getAdapter().getItemCount();
        characters.smoothScrollToPosition(previousSize);
        Thread.sleep(2000);
        int newSize = characters.getAdapter().getItemCount();
        onView(withId(R.id.characters)).perform(RecyclerViewActions.scrollToPosition(previousSize));
        assertNotSame(previousSize, newSize);
    }

    @Test
    public void openActivityCharacter () {
        onView(withId(R.id.characters))
                .perform(
                        RecyclerViewActions
                                .actionOnItemAtPosition(0, click()));

        assertFalse(mActivity.isRunning());
    }
}

1 个答案:

答案 0 :(得分:0)

我会使用AndroidTestCase和我的GUI使用ActivityInstrumentationTestCase2测试我的后端(使用后端模拟)