我正在尝试以 TDD 式的方式创建一个服务,为此我创建了以下测试。该服务主要轮询 Web 服务并将新信息放入内容提供程序中。由于它是一项服务,因此我使用内容提供程序,它将将信息存储到其中作为测试的预言机。
我认为我想要做的是创建一个 MockContentResolver 来实现此目的,但在 ProviderTestCase2 类之外缺乏它的示例。然而,当我运行此脚本时,它在 addProvider 行上为空指针。
有人有创建/访问模拟内容解析器的示例吗?在服务测试用例中?
public class OnDemandPollingServiceTests extends ServiceTestCase<OnDemandJobFetchingService> {
private MockContentResolver mContentResolver;
public OnDemandPollingServiceTests() {
super(OnDemandJobFetchingService.class);
}
protected void setUp() throws Exception {
super.setUp();
mContext = getContext();
ContentProvider cp = new OnDemandJobInfoProvider();
mContentResolver.addProvider(OnDemandJobInfoProvider.AUTHORITY, cp);
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testJobInsertion() {
Uri url = Jobs.JobsColumns.CONTENT_URI;
Cursor cursor;
cursor = mContentResolver.query(url, null, null, null, null);
int before = cursor.getCount();
cursor.close();
Intent startIntent = new Intent();
startIntent.setClass(mContext, OnDemandJobFetchingService.class);
startService(startIntent);
cursor = mContentResolver.query(url, null, null, null, null);
int after = cursor.getCount();
cursor.close();
assertTrue(before != after);
}
}
对我来说,你似乎从未实例化过你的mContentResolver
(你没有像这样的行mContentResolver = new MockContentResolver();
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)