0

I created a custom ContentProvider, which implemented by Sqlite query, update, insert etc. And in my code, I used this ContentProvider to store my datas. Now I want to use Robolectric to unit test my app. But the test code will fail as below,

java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.podcast.rick.database.PodcastProvider.query(PodcastProvider.java:61)
at org.robolectric.shadows.ShadowContentResolver.query(ShadowContentResolver.java:157)
at android.content.ContentResolver.query(ContentResolver.java)
at com.podcast.rick.rpodcast.RssReaderTest.parseRSS(RssReaderTest.java:61)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:487)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:209)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:63)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

it can be seen that Robolectric shadow class called first,

at org.robolectric.shadows.ShadowContentResolver.query(ShadowContentResolver.java:157)  
at android.content.ContentResolver.query(ContentResolver.java)

but finally android code called,

android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)

I think in the Robolectric unit test, the shadow class should be used instead of Android framework code, but here why android.database.sqlite.SQLiteOpenHelper called ?

My setUp method is below,

@Before
public void setUp() throws Exception {
   PodcastProvider provider = new PodcastProvider();
   provider.onCreate();
   Robolectric.setupContentProvider(PodcastProvider.class);
   ShadowContentResolver.registerProviderInternal("com.rick.podcast", provider);
}

Anything wrong with it or anything missing? I'm using Robolectric 3.3.1.

Thanks.

RickSu
  • 1
  • 2

1 Answers1

0

Make sure you have annotated your test class with

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class YourTestClass { ...
Youness
  • 1,920
  • 22
  • 28