【发布时间】:2015-05-28 14:41:53
【问题描述】:
我是单元测试的新手,我想测试我的 SQLiteDataBase。
我有一个名为 MySQLiteHelper 的类,它扩展了 SQLiteOpenHelper。 我有一个名为 LocationDataHandler 的类,用于在我的数据库中添加或删除元素。 我有一个类 LocationDataHandlerTest,它扩展了 AndroidTestCase,来测试我的 LocationDataHandler 类。
我正在尝试测试我的 SQLite 数据库,但我似乎有点迷失在所有不同的上下文之间。
这是我的代码:
//Context context = new Activity();
//IsolatedContext context = getMockContext();
//Context context = new MockContext();
//Context context = getInstrumentation().getContext();
Context context = getContext();
helper = new MySQLiteHelper(context);
assertNotNull(helper);
SQLiteDatabase db = helper.getWritableDatabase();
assertNotNull(db); <---- it fails here !
如您所见,我尝试了许多不同的环境,我看到人们使用和使用这些环境。我真的不明白为什么它在我的情况下不起作用。
我的问题是测试在“assertNotNull(db);”行上失败了这意味着我永远无法检索我的数据库。
我做错了吗?我没有使用正确的上下文吗?
编辑:这是我的日志:
junit.framework.AssertionFailedError
at junit.framework.Assert.fail(Assert.java:55)
at junit.framework.Assert.assertTrue(Assert.java:22)
at junit.framework.Assert.assertNotNull(Assert.java:256)
at junit.framework.Assert.assertNotNull(Assert.java:248)
at junit.framework.TestCase.assertNotNull(TestCase.java:417)
at com.databerries.LocationDataHandlerTest.testAddLocation(LocationDataHandlerTest.java:72)
【问题讨论】:
-
可以添加logcat吗?只是为了详细了解您的问题..
-
你试过
getInstrumentation.getTargetContext()吗? -
是的,它给我抛出了一个空指针异常:
Context context = getInstrumentation().getTargetContext(); -
我也有同样的问题。你找到解决办法了吗?
-
幸运吗?我也卡在那部分,如果您有任何解决方案,请分享一下
标签: java android sqlite unit-testing