MockContentResolver在ServiceTestCase空指针 [英] MockContentResolver in a ServiceTestCase null pointers

查看:466
本文介绍了MockContentResolver在ServiceTestCase空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个TDD上下的方式服务,并为此我创建了下面的测试。基本服务轮询Web服务,并把新的信息内容提供商。既然是服务,我使用,这将是存储信息到作为测试神谕的内容提供商。

我想我想要做的是为了实现这一点,但还缺乏一个ProviderTestCase2类以外的这样的例子创建MockContentResolver。当我运行该脚本但它它放在addProvider行空指针。

有没有人有创建/访问嘲弄了内容解析的例子吗?在ServiceTestCase?

 公共类OnDemandPollingServiceTests延伸ServiceTestCase< OnDemandJobFetchingService> {
  私人MockContentResolver mContentResolver;  公共OnDemandPollingServiceTests(){
        超(OnDemandJobFetchingService.class);
    }  保护无效设置()抛出异常{
    super.setUp();
    mContext =的getContext();    ContentProvider的CP =新OnDemandJobInfoProvider();
    mContentResolver.addProvider(OnDemandJobInfoProvider.AUTHORITY,CP);
  }  保护无效拆解()抛出异常{
    super.tearDown();
  }  公共无效testJobInsertion(){
    乌里URL = Jobs.JobsColumns.CONTENT_URI;
    光标光标;
    光标= mContentResolver.query(URL,NULL,NULL,NULL,NULL);
    之前= cursor.getCount INT();
    cursor.close();    意图startIntent =新的Intent();
    startIntent.setClass(mContext,OnDemandJobFetchingService.class);
    startService(startIntent);    光标= mContentResolver.query(URL,NULL,NULL,NULL,NULL);
    后= cursor.getCount INT();
    cursor.close();
    assertTrue(前=后!);
  }
}


解决方案

对我来说,好像你从来没有实例化你的 mContentResolver (你不必像一条线 mContentResolver =新MockContentResolver();

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);
  }
}

解决方案

To me it seems like you have never instantiated your mContentResolver (you don't have a line like mContentResolver = new MockContentResolver();.

这篇关于MockContentResolver在ServiceTestCase空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆