4
我试图以TDD-ish方式创建服务,为此我创建了以下测试。该服务基本上轮询Web服务并将新信息放入Content Provider中。由于它是一项服务,我正在使用Content Provider来存储信息,作为测试的预测。ServiceTestCase中的MockContentResolver空指针
我想我想要做的是创建一个MockContentResolver为了实现这一点,但缺乏一个ProviderTestCase2类以外的例子。但是,当我运行此脚本时,它在addProvider行上为空指针。
有没有人有创建/访问模拟出内容解析器的例子?在一个ServiceTestCase中?
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);
}
}