1

我正在尝试为 ActivityUnitTestCase 中的 Activity 提供模拟服务。我已经包装了目标上下文,我试图截获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);
    }
};

但是,服务无法启动,因为该组件未在清单中列出:

无法启动服务 Intent { cmp=com.myapp.android.test/com.myapp.android.service.MockService } U=0:未找到

据我所知,测试应用程序没有清单。有什么方法可以模拟 ActivityUnitTestCase 的服务吗?

4

0 回答 0