Search code examples
javaandroidtestingandroid-contentprovider

NPE in setUp() method for a ProviderTestCase subclass


I currently have the following setUp() method in my ProviderTestCase2. It throws an NPE deep inside the Android API after the call to newResolverWithContentProviderFromSql(). What am I doing wrong and how do I fix it?

public void setUp() throws IllegalAccessException, InstantiationException {
    this.resolver = newResolverWithContentProviderFromSql(this.getMockContext(),
            "test.", BaseballCardProvider.class,
            BaseballCardContract.AUTHORITY,
            BaseballCardSQLHelper.DATABASE_NAME,
            BaseballCardSQLHelper.SCHEMA_VERSION, CREATE_TABLE
                    + INSERT_DATA);
}

Here's the stack trace for reference:

java.lang.NullPointerException  
at android.test.RenamingDelegatingContext.openOrCreateDatabase(RenamingDelegatingContext.java:146)  
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:215)  
at android.database.DatabaseUtils.createDbFromSqlStatements(DatabaseUtils.java:1315)  
at android.test.ProviderTestCase2.newResolverWithContentProviderFromSql(ProviderTestCase2.java:219)  
at bbct.android.common.provider.test.BaseballCardProviderTest.setUp(BaseballCardProviderTest.java:44)  
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)  
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)  
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)  
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)

Solution

  • Since you have overriden setUp() but not called up to super.setUp(), the TestCase isn't complete because like the documentation says:

    This framework is set up automatically by the base class' setUp() method. If you override this method, you must call the super method as the first statement in your override.