我正在尝试为ActivityUnitTestCase中的Activity提供模拟服务。我已经包装了目标Context,我试图拦截startService()
消息并转换ComponentName,使其指向我的Mocked服务:
// Wrap the Context and use a Service that reports whether methods have been called:
Context targetContext = new ContextWrapper(mInstrumentation.getTargetContext()) {
@Override
public ComponentName startService(Intent service) {
// point to the mock appservice we want
if ("com.myapp.android.service.AppService".equals(service.getComponent().getClassName())) {
service.setComponent(new ComponentName(mInstrumentation.getContext(), MockService.class.getName()));
} else {
fail("Unknown service started!");
}
return super.startService(service);
}
};
但是,无法启动服务,因为该组件未在Manifest中列出:
无法启动服务Intent {cmp = com.myapp.android.test / com.myapp.android.service.MockService} U = 0:未找到
据我所知,测试应用程序没有清单。有没有办法模拟ActivityUnitTestCase的服务?