ServiceTestCase 中的 MockContentResolver 空指针

发布于 2024-11-27 02:16:14 字数 1358 浏览 0 评论 0原文

我正在尝试以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2024-12-04 02:16:14

对我来说,您似乎从未实例化过您的 mContentResolver (您没有像 mContentResolver = new MockContentResolver(); 这样的行。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文