Query using MockContentResolver leads to NullPointerException

ctaymor

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.

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

Later on, in our tests, when calling the following code, we get a NullPointerException:

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

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

We have verified that mActivity is not null.

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().

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?

BadIdeaException

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

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

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).

Hope this helps you!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Enabling Hystrix leads to NullPointerException when using kotlin with spring-boot

From Dev

ProGuard leads to NullPointerException

From Java

Using Both Query Api And Criteria Api in Hibernate Leads Problems

From Java

Using #{principal} in @Query leads to "Property or field 'principal' cannot be found on null"

From Java

Optional in Java conditional operator leads to NullPointerException

From Dev

.getSupportActionBar not available in Fragment; workaround leads to NullPointerException on rotation

From Dev

@Email validation-annotation leads to NullPointerException in deployment

From Dev

Does EF not using the same old concept of creating large query that leads to degrade performance?

From Dev

Select a query within a query that leads to opposite result

From Java

@FXML Injection of custom component does not instantiate variable in Controller and leads to NullPointerException

From Dev

Save to excel file leads to java.lang.NullPointerException

From Dev

Dependency injection with Play Framework and Guice leads to NullPointerException on Application start

From Dev

Query Enum column in sqlalchemy leads to LookupError

From Dev

NullPointerException for Parse Query

From Dev

sshing using gcloud leads to agent refusal

From Dev

Using only constraint in owl leads to inconsistency

From Dev

Using the range operator on lists leads to infinite expansion

From Dev

Using QualifierFilter leads to only returning matched columns

From Dev

Inheritance leads to error when using reference class

From Dev

Using IAmsiStream from .Net leads to AccessViolationException

From Dev

Convert Leads to Opportunity using Salesforce sdk for iOS

From Dev

WPF view who leads to another using MVVM

From Dev

Using external variable leads to unexpected object

From Dev

Creating Leads in Marketo using REST API

From Dev

Using while or if as a condition for recursion leads to different results

From Dev

Select random Leads records using SOQL (Salesforce)

From Dev

Using common constants module leads to circular import

From Dev

Swift: Using `var` leads to a compiler warning, using `let` leads to compiler errors?

From Mysql

Inserting into MySQL with python via parametrized query leads to error

Related Related

  1. 1

    Enabling Hystrix leads to NullPointerException when using kotlin with spring-boot

  2. 2

    ProGuard leads to NullPointerException

  3. 3

    Using Both Query Api And Criteria Api in Hibernate Leads Problems

  4. 4

    Using #{principal} in @Query leads to "Property or field 'principal' cannot be found on null"

  5. 5

    Optional in Java conditional operator leads to NullPointerException

  6. 6

    .getSupportActionBar not available in Fragment; workaround leads to NullPointerException on rotation

  7. 7

    @Email validation-annotation leads to NullPointerException in deployment

  8. 8

    Does EF not using the same old concept of creating large query that leads to degrade performance?

  9. 9

    Select a query within a query that leads to opposite result

  10. 10

    @FXML Injection of custom component does not instantiate variable in Controller and leads to NullPointerException

  11. 11

    Save to excel file leads to java.lang.NullPointerException

  12. 12

    Dependency injection with Play Framework and Guice leads to NullPointerException on Application start

  13. 13

    Query Enum column in sqlalchemy leads to LookupError

  14. 14

    NullPointerException for Parse Query

  15. 15

    sshing using gcloud leads to agent refusal

  16. 16

    Using only constraint in owl leads to inconsistency

  17. 17

    Using the range operator on lists leads to infinite expansion

  18. 18

    Using QualifierFilter leads to only returning matched columns

  19. 19

    Inheritance leads to error when using reference class

  20. 20

    Using IAmsiStream from .Net leads to AccessViolationException

  21. 21

    Convert Leads to Opportunity using Salesforce sdk for iOS

  22. 22

    WPF view who leads to another using MVVM

  23. 23

    Using external variable leads to unexpected object

  24. 24

    Creating Leads in Marketo using REST API

  25. 25

    Using while or if as a condition for recursion leads to different results

  26. 26

    Select random Leads records using SOQL (Salesforce)

  27. 27

    Using common constants module leads to circular import

  28. 28

    Swift: Using `var` leads to a compiler warning, using `let` leads to compiler errors?

  29. 29

    Inserting into MySQL with python via parametrized query leads to error

HotTag

Archive