2

我在测试用例结束时关闭一个 Activity,以便在下一个测试用例开始时它为空:

public void test1() throws Exception {

    //activity is calling the RegisterActivity in onCreate
    activity = getActivity();

    // register next activity that need to be monitored.
    RegisterActivity nextActivity = (RegisterActivity) getInstrumentation()
        .waitForMonitorWithTimeout(activityMonitor, 5);
    // next activity is opened and captured.
    assertNotNull(nextActivity);
    if (nextActivity != null) {
        nextActivity.finish();
    }
}

public void test2() throws Exception {
    //nextActivity.finish() from the previous test has not yet finished

    //body of test2
    //...
    //...
}

如果我在 test1 中设置了一个线程睡眠,那么问题就解决了:

    if (nextActivity != null) {
        nextActivity.finish();
        Thread.sleep(1500);
    }

有更好的方法吗?有没有一种方法可以阻止 TestRunner 直到 nextActivity 完成?

4

1 回答 1

0

尝试为 nextActivity 创建一个new thread运行。调用该thread.start()方法后,调用thread.join()您希望 TestRunner 阻塞的位置。

于 2013-11-06T17:11:45.103 回答