我创建了一个自定义的ContentProvider,它由Sqlite查询,更新,插入等实现。在我的代码中,我使用这个ContentProvider来存储我的数据。 现在我想用Robolectric对我的应用进行单元测试。 但测试代码将失败,如下所示,
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)
可以看出Robolectric影子类首先调用,
at org.robolectric.shadows.ShadowContentResolver.query(ShadowContentResolver.java:157)
at android.content.ContentResolver.query(ContentResolver.java)
但最后调用了android代码,
android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
我认为在Robolectric单元测试中,应该使用shadow类代替Android框架代码,但这里为什么调用android.database.sqlite.SQLiteOpenHelper?
我的setUp方法如下,
@Before
public void setUp() throws Exception {
PodcastProvider provider = new PodcastProvider();
provider.onCreate();
Robolectric.setupContentProvider(PodcastProvider.class);
ShadowContentResolver.registerProviderInternal("com.rick.podcast", provider);
}
有什么问题或遗漏了什么? 我使用的是Robolectric 3.3.1。
感谢。
答案 0 :(得分:0)
确保您已使用
注释了您的测试类@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class YourTestClass { ...