查询使用MockContentResolver导致NullPointerException异常 [英] Query using MockContentResolver leads to NullPointerException

查看:542
本文介绍了查询使用MockContentResolver导致NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有延伸一个JUnit测试类 ActivityInstrumentationTestCase2< CommentActivity> 。测试(我们正在测试的类)中使用 CommentContentProvider ,它扩展了的ContentProvider ,访问SQLite数据库,而我们得到一个 NullPointerException异常 [以下完整的堆栈跟踪]上运行的提供者查询时。

We have a JUnit test class which extends ActivityInstrumentationTestCase2<CommentActivity>. The test (and the class we're testing) use CommentContentProvider, which extends ContentProvider, to access the SQLite database, and we're getting a NullPointerException [full stack trace below] when running a query on the provider.

我们实例化一个 MockContentResolver 如下所示:

We instantiate a MockContentResolver as shown:

MockContentResolver mResolver;

public void setUp() {
    super.setUp();
    CommentContentProvider ccp = new CommentContentProvider();
    mResolver = new MockContentResolver();
    mResolver.addProvider(CommentContentProvider.AUTHORITY, ccp);
}

后来,在我们的测试中,调用下面的code的时候,我们得到了一个 NullPointerException异常

Cursor mCursor = mResolver.query(Uri.parse(mUri), null, null, null, null);

我们得到相同的结果,即使我们迫不及待的实例 MockContentResolver ,直到我们有测试中的副本活动:

We get the same result even if we wait to instantiate MockContentResolver until we have a copy of the activity under test:

mActivity = getActivity();
MockContentResolver mResolver = new MockContentResolver(mActivity);

我们已经验证 mActivity 不为空。

一个同事通过Android源代码(在我们的系统没有安装)加强和发现错误的直接原因是,的getContext()返回上的<空href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/content/ContentProvider.java#ContentProvider.enforceReadPermissionInner%28android.net.Uri%29"相对=nofollow> ContentProvider.enforceReadPermissionInner的第一行()。

A colleague stepped through the Android source (not installed on our system) and found that the proximate cause of the error is that getContext() returns null on the first line of ContentProvider.enforceReadPermissionInner().

我们看了看<一href="http://stackoverflow.com/questions/14987309/android-activityinstrumentationtestcase2-nullpointerexception-on-getting-context">this问题原本看起来相似,但我认为这是一个不同的问题完全。 <一href="http://stackoverflow.com/questions/6853207/mockcontentresolver-in-a-servicetestcase-null-pointers">This问题也是一个问题类似的症状,但他们并没有实例化的 MockContentResolver 。我们有问题实例我们的。

We took a look at this question which originally seemed similar, but I think it was a different problem entirely. This question is also a similar symptom of a problem, but they didn't instantiate their MockContentResolver. We are having problems instantiating ours.

下面是我们得到的堆栈跟踪:

Here's the stack trace we're getting:

java.lang.NullPointerException
at android.content.ContentProvider$Transport.enforceReadPermissionInner(ContentProvider.java:449)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:394)
at android.content.ContentProvider$Transport.query(ContentProvider.java:194)
at android.content.ContentResolver.query(ContentResolver.java:461)
at android.content.ContentResolver.query(ContentResolver.java:404)
at packagename.test.FooActivityTest.getNumCommentsForRecipient(FooActivityTest.java:84)
at packagename.test.FooActivityTest.testCommentEntryInternal(FooActivityTest.java:91)
at packagename.test.FooActivityTest.testCommentEntry1(FooActivityTest.java:108)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.access$000(InstrumentationTestCase.java:36)
at android.test.InstrumentationTestCase$2.run(InstrumentationTestCase.java:189)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1719)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)

我们如何解决这个问题呢?

How can we resolve this problem?

推荐答案

我有一个类似的问题,测试的内容提供商内部依赖另一个内容提供商写远一些元数据的时候。

I had a similar problem when testing a content provider that internally relied on another content provider to write away some metadata.

首先,你可能会更好使用<一个href="http://developer.android.com/reference/android/test/ProviderTestCase2.html">ProviderTestCase2类,它会做的大部分工作被测建立提供给你。它可能使你的生活相当容易。 (对我来说,这是不够的,因为它只会帮你一个供应商,我需要两个。)

First of all, you may be better off using the ProviderTestCase2 class, which will do most of the work for setting up the provider under test for you. It might make your life considerably easier. (For me this wasn't enough because it'll only help you with one provider, I needed two.)

如果这是不可能的你,这里是没有的伎俩对我来说:

If this is not possible for you, here's what did the trick for me:

您的查询失败,因为你的供应商从来没有连接到它的上下文。你必须自己做,手动 - 该文件忘记提及。做到这一点:

Your query fails because your provider never had a context attached to it. You have to do this yourself, manually - which the documentation forgets to mention. Do this:

public void setUp() {
    super.setUp();
    CommentContentProvider ccp = new CommentContentProvider();

    // Add this line to attach context:
    ccp.attachInfo(mActivity, null);

    mResolver = new MockContentResolver();
    mResolver.addProvider(CommentContentProvider.AUTHORITY, ccp);
}

我不是100%肯定要重视这方面,让您的测试从世界其他地区隔离, ProviderTestCase2 设置模拟环境中的整个链条。如果您有问题,看<一href="http://developer.android.com/reference/android/test/RenamingDelegatingContext.html"><$c$c>RenamingDelegatingContext和<一href="http://developer.android.com/reference/android/test/IsolatedContext.html"><$c$c>IsolatedContext,这些是那些 ContentProviderTestCase2 使用。 (有一个在其设置()法)。

I'm not 100% sure which context to attach to keep your test isolated from the rest of the world, ProviderTestCase2 sets up a whole chain of mock contexts. If you're having issues, look at RenamingDelegatingContext and IsolatedContext, those are the ones ContentProviderTestCase2 uses. (Have a look at its setUp() method).

希望这可以帮助你!

这篇关于查询使用MockContentResolver导致NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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