用于ProviderTestCase子类的setUp()方法中的NPE

时间:2013-12-07 22:16:06

标签: java android testing android-contentprovider

我目前在setUp()中使用了以下ProviderTestCase2方法。在调用newResolverWithContentProviderFromSql()后,它会在Android API内部抛出一个NPE。我做错了什么,如何解决?

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

这是堆栈跟踪供参考:

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)

1 个答案:

答案 0 :(得分:2)

由于您已覆盖setUp()但未调用super.setUp(),因此TestCase未完成,因为documentation表示:

  

此框架由基类'setUp()自动设置   方法。如果重写此方法,则必须将super方法调用为   覆盖中的第一个语句。