Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用Robolectric的ContentProvider测试_Android_Unit Testing_Robolectric - Fatal编程技术网

Android 使用Robolectric的ContentProvider测试

Android 使用Robolectric的ContentProvider测试,android,unit-testing,robolectric,Android,Unit Testing,Robolectric,我创建了一个自定义ContentProvider,它由Sqlite查询、更新、插入等实现。在我的代码中,我使用这个ContentProvider来存储数据。 现在我想用Robolectric对我的应用程序进行单元测试。 但测试代码将失败,如下所示: java.lang.NullPointerException at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223) at an

我创建了一个自定义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)
可以看出,机器人分子阴影类称为first

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呢

我的设置方法如下:

@Before
public void setUp() throws Exception {
   PodcastProvider provider = new PodcastProvider();
   provider.onCreate();
   Robolectric.setupContentProvider(PodcastProvider.class);
   ShadowContentResolver.registerProviderInternal("com.rick.podcast", provider);
}
有什么不对劲的地方吗? 我用的是机器人3.3.1


谢谢。

请确保您已经用

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class YourTestClass { ...

请将日志也放在灰色代码块中。请先自己对该异常进行评论。