问题描述
我是新来的单元测试,我想测试我的SQLiteDataBase。
我有一类名为 MySQLiteHelper 扩展 SQLiteOpenHelper 。我有一类名为 LocationDataHandler ,我用它来从我的数据库中添加或删除元素。我有一个类 LocationDataHandlerTest ,可扩展的 AndroidTestCase ,来测试我的 LocationDataHandler 类。
我想测试我的SQLite数据库,但我似乎所有不同的上下文之间要一点点丧失。
这是我的code:
//上下文语境=新的活动();
// IsolatedContext上下文= getMockContext();
//上下文语境=新MockContext();
//上下文语境= getInstrumentation()的getContext()。
上下文的背景下=的getContext();
辅助=新MySQLiteHelper(上下文);
assertNotNull(辅助);
SQLiteDatabase DB = helper.getWritableDatabase();
assertNotNull(DB); < ----它失败了!
你可以看到我已经尝试过,我看到人们使用和处理的许多不同的环境。我真的不明白为什么它不会在我的情况下工作。
我的问题是,测试失败就行了assertNotNull(分贝);这意味着我永远无法找回我的数据库。
难道我这做了错误的方式?难道我没有使用正确的背景下?
修改:这是我的logcat:
junit.framework.AssertionFailedError
在junit.framework.Assert.fail(Assert.java:55)
在junit.framework.Assert.assertTrue(Assert.java:22)
在junit.framework.Assert.assertNotNull(Assert.java:256)
在junit.framework.Assert.assertNotNull(Assert.java:248)
在junit.framework.TestCase.assertNotNull(TestCase.java:417)
在com.databerries.LocationDataHandlerTest.testAddLocation(LocationDataHandlerTest.java:72)
有关测试与Android工作室,
您应该使用 MockContext
而不是 RenamingDelegatingContext
。
使用 InstrumentationTestCase
。
有关详细信息,请参考这个答案。 http://stackoverflow.com/a/29063736/1020456
下面是我的code。
公共类DatabaseHelperTest扩展InstrumentationTestCase {
私人DatabaseHelper分贝;
@之前
公共无效设置()抛出异常{
super.setUp();
MockContext上下文=新MockContext();
DB = DatabaseHelper.getInstance(上下文);
}
@后
公共无效的teardown()抛出异常{
db.close();
super.tearDown();
}
公共无效testInsertUserInteraction()抛出异常{
assertNotNull(db.getReadableDatabase()); //应该通过
}
}
I'm new to Unit testing and I want to test my SQLiteDataBase.
I have a class named MySQLiteHelper that extends SQLiteOpenHelper.I have a class named LocationDataHandler that I use to add or delete elements from my DataBase.And I have a class LocationDataHandlerTest, that extends AndroidTestCase, to test my LocationDataHandler class.
I'm trying to test my SQLite Database but I seem to be a little bit lost between all the different contexts.
here is my code :
//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 !
as you can see I tried with many different contexts that i saw people use and work with. I really don't understand why it doesn't work in my case.
My problem is that the test is failing on the line "assertNotNull(db);" meaning i can never retrieve my dataBase.
Am I doing this the wrong way ? Am I not using the right context ?
Edit : Here is my logcat :
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)
For testing with Android Studio,
You should use MockContext
instead of RenamingDelegatingContext
.
Use InstrumentationTestCase
.
For further information, see this answer. http://stackoverflow.com/a/29063736/1020456
Here is my code.
public class DatabaseHelperTest extends InstrumentationTestCase{
private DatabaseHelper db;
@Before
public void setUp() throws Exception {
super.setUp();
MockContext context = new MockContext();
db = DatabaseHelper.getInstance(context);
}
@After
public void tearDown() throws Exception {
db.close();
super.tearDown();
}
public void testInsertUserInteraction() throws Exception {
assertNotNull(db.getReadableDatabase()); //should pass
}
}
这篇关于Android的工作室单元测试SQLiteDataBase为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!