我正在尝试以一种类似于TDD的方式创建服务,为此,我创建了以下测试。该服务主要是轮询Web服务,并将新信息放入内容提供者。因为它是一项服务,所以我使用它将存储信息的内容提供商作为测试的先知。
我想我想要做的是创建一个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);
}
}发布于 2011-11-09 22:06:27
在我看来,您似乎从未实例化过您的mContentResolver (您没有像mContentResolver = new MockContentResolver();这样的行。
https://stackoverflow.com/questions/6853207
复制相似问题