为 junit 测试设置 greenDAO 会话对象
Setting up greenDAO session object for a junit test
我正在尝试创建一个 greenDAO
数据库会话对象用于我的 junit
测试。当我尝试获取 SQLiteDatabase
对象时,我总是得到 null。
没有返回错误,我不知道为什么。
代码下方:
@RunWith(MockitoJUnitRunner.class)
public class ChatRoomModuleTest {
SomeEntityDao someEntityDao;
@Mock
Context mMockContext;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Before
public void Before(){
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(mMockContext, "myapp-db", null);
SQLiteDatabase db = openHelper.getWritableDatabase(); //always return null;
DaoSession daoSession = new DaoMaster(db).newSession();
someEntityDao = daoSession.getSomeEntityDao();
}
}
注意:我知道我可以使用 android 测试来测试它,但是它们要慢得多并且不需要测试独立平台逻辑。
找到解决方案并涉及几个步骤:
1) 使用 Robolectric 包来创建应用程序
在 app.gradle 添加:
//if your project use multidex
testCompile "org.robolectric:shadows-multidex:3.0"
//otherwise use
//testCompile 'org.robolectric:robolectric:3.1'
2) 像这样构建测试 class:
@RunWith(RobolectricGradleTestRunner.class) //run test with roboteletric
@Config(constants = BuildConfig.class, sdk = 19)
public class test {
MyEntityDao myEntityDao;
DaoSession daoSession;
@Before
public void setUp() {
//use roboteletric to create a valid Application Object
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(RuntimeEnvironment.application, null, null);
SQLiteDatabase db = openHelper.getWritableDatabase();
Assert.assertNotNull(db);
daoSession = new DaoMaster(db).newSession();
myEntityDao = daoSession.getMyEntityDao();
}
@Test
public void t1() {
MyEntity myEntity = new MyEntity();
myEntityDao.insert(MyEntity);
}
}
3) 最后将工作目录定义为已发布的 用于测试,以便 Robolectric 可以找到项目清单文件。
我不确定此时是否会在其他测试中始终如一地工作,也许最好改用 android 测试。 GreenDao 似乎不适合在 android 之外使用。
我正在尝试创建一个 greenDAO
数据库会话对象用于我的 junit
测试。当我尝试获取 SQLiteDatabase
对象时,我总是得到 null。
没有返回错误,我不知道为什么。
代码下方:
@RunWith(MockitoJUnitRunner.class)
public class ChatRoomModuleTest {
SomeEntityDao someEntityDao;
@Mock
Context mMockContext;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Before
public void Before(){
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(mMockContext, "myapp-db", null);
SQLiteDatabase db = openHelper.getWritableDatabase(); //always return null;
DaoSession daoSession = new DaoMaster(db).newSession();
someEntityDao = daoSession.getSomeEntityDao();
}
}
注意:我知道我可以使用 android 测试来测试它,但是它们要慢得多并且不需要测试独立平台逻辑。
找到解决方案并涉及几个步骤:
1) 使用 Robolectric 包来创建应用程序
在 app.gradle 添加:
//if your project use multidex
testCompile "org.robolectric:shadows-multidex:3.0"
//otherwise use
//testCompile 'org.robolectric:robolectric:3.1'
2) 像这样构建测试 class:
@RunWith(RobolectricGradleTestRunner.class) //run test with roboteletric
@Config(constants = BuildConfig.class, sdk = 19)
public class test {
MyEntityDao myEntityDao;
DaoSession daoSession;
@Before
public void setUp() {
//use roboteletric to create a valid Application Object
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(RuntimeEnvironment.application, null, null);
SQLiteDatabase db = openHelper.getWritableDatabase();
Assert.assertNotNull(db);
daoSession = new DaoMaster(db).newSession();
myEntityDao = daoSession.getMyEntityDao();
}
@Test
public void t1() {
MyEntity myEntity = new MyEntity();
myEntityDao.insert(MyEntity);
}
}
3) 最后将工作目录定义为已发布的
我不确定此时是否会在其他测试中始终如一地工作,也许最好改用 android 测试。 GreenDao 似乎不适合在 android 之外使用。