我在测试用例结束时关闭一个活动,因此它在下一个测试用例的开头为 null:
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);
}
有没有更好的方法?有没有一种方法可以阻止测试运行程序,直到下一个活动完成?
尝试为下一个要运行的 Activity 创建一个new thread
。调用 thread.start()
方法后,调用要阻止 TestRunner 的thread.join()
。