Cri*_*ian 6 android unit-testing
我有一个测试用例,它使用Instrumentation.ActivityMonitor来检查是否发送了Intent.当测试成功时,这可以正常工作.如果断言失败,则下一个测试用例会挂起setUp()中的getActivity()调用.
我应该打个电话来清理吗?
它似乎围绕一个Activity启动,但ActivityMonitor没有抓住它.也就是说,没有触发IntentFilter.测试失败,但新的Activity永远不会被解雇,似乎会干扰下一个getActivity()调用.
这个问题类似于另一个问题,但是这个解决方案(调用super.tearDown())并没有解决我的问题.
public class SimpleActivityTest
extends ActivityInstrumentationTestCase2<SimpleActivity> {
private SimpleActivity activity;
@Override
protected void setUp() throws Exception {
super.setUp();
this.getInstrumentation().setInTouchMode(false);
Intent intent = new Intent();
intent.putExtra("DATA_ITEM_1", 1);
intent.putExtra("DATA_ITEM_2", 2);
this.setActivityIntent(intent);
this.activity = getActivity(); // this call hangs on second test
}
public void testOtherActivityCalled() {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_VIEW);
ifilter.addDataScheme("http");
ifilter.addDataAuthority("some.domain.com", null);
ifilter.addDataPath("foobar", PatternMatcher.PATTERN_PREFIX);
ActivityMonitor activityMonitor = getInstrumentation().addMonitor(
ifilter, null, false);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// launch other activity somehow
}
});
getInstrumentation().waitForIdleSync();
Activity otherActivity = activityMonitor.waitForActivityWithTimeout(2000);
assertNotNull(otherActivity);
otherActivity.finish();
}
public void testSomethingElse() {
// This code will never run because getActivity() in setUp() will
// never return
}
}
Run Code Online (Sandbox Code Playgroud)
我怀疑您创建的 Runnable 永远不会退出。由于它在 UI 线程上运行,因此它永远不会允许 Activity 生命周期执行其需要的操作。您从 UI 线程执行此操作有什么特殊原因吗?
归档时间: |
|
查看次数: |
2129 次 |
最近记录: |