ServiceTestCase 中的 MockContentResolver 空指针
我正在尝试以 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);
}
}
I'm trying to create a Service in a TDD-ish manner and to that end I have created the following test. The service basically polls a Web Service and puts new information into a Content Provider. Since it is a service, I am using the Content Provider that it will be storing information into as the oracle of the test.
I think what I want to do is create a MockContentResolver in order to achieve this but there is a lack of examples of it outside of a ProviderTestCase2 class. When I run this script however it it null pointers on the addProvider line.
Does anyone have an example of creating/accessing a mocked out content resolver? In a 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,您似乎从未实例化过您的
mContentResolver
(您没有像mContentResolver = new MockContentResolver();
这样的行。To me it seems like you have never instantiated your
mContentResolver
(you don't have a line likemContentResolver = new MockContentResolver();
.