![](/img/trans.png)
[英]Make wait another method until coroutine finishes [Android Kotlin]
[英]How to make Android TestRunner wait until activity finishes
我在一个测试用例的结尾处关闭一个Activity,因此在下一个测试用例的开头它为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);
}
有更好的方法吗? 有没有一种方法可以阻止TestRunner,直到nextActivity完成?
尝试为要运行的nextActivity创建一个new thread
。调用thread.start()
方法后,在要阻止TestRunner的位置调用thread.join()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.