我有一个测试,创建了一个活动并试图从数据库中获取一些数据。然而这个操作失败了,出现了SQLiteException错误。
这在我将数据库类移动到单例模型之前很好地工作。请问有什么建议可以处理Robolectric吗? 我找不到任何关于这个的文档或样例。
编辑: 运行Robolectric 3.0 RC-2。 Robolectric正在驱动我的活动,尝试对数据库进行一些操作。 在我的应用程序DB类中,从下面的实例检查中删除instance == null的检查'修复'了此问题(即,如果MySQLiteOpenHelper每次重新创建,则Robolectric运行测试时没有问题)。
MySQLiteOpenHelper是SQLiteOpenHelper的一个简单扩展。
故障发生在(再次提醒,这是在我的数据库类内部):
17:40:40.528 [DEBUG] [TestEventLogger] android.database.sqlite.SQLiteException: Cannot open SQLite connection, base error code: 14
17:40:40.528 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection.rethrow(ShadowSQLiteConnection.java:53)
17:40:40.528 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection.access$600(ShadowSQLiteConnection.java:30)
17:40:40.529 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection$Connections.execute(ShadowSQLiteConnection.java:443)
17:40:40.529 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection$Connections.open(ShadowSQLiteConnection.java:345)
17:40:40.529 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection.nativeOpen(ShadowSQLiteConnection.java:58)
17:40:40.529 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnection.nativeOpen(SQLiteConnection.java)
17:40:40.529 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
17:40:40.529 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
17:40:40.529 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
17:40:40.530 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
17:40:40.530 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
17:40:40.530 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
17:40:40.530 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
17:40:40.530 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
17:40:40.530 [DEBUG] [TestEventLogger] at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1142)
17:40:40.530 [DEBUG] [TestEventLogger] at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:267)
17:40:40.531 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
17:40:40.531 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
这在我将数据库类移动到单例模型之前很好地工作。请问有什么建议可以处理Robolectric吗? 我找不到任何关于这个的文档或样例。
编辑: 运行Robolectric 3.0 RC-2。 Robolectric正在驱动我的活动,尝试对数据库进行一些操作。 在我的应用程序DB类中,从下面的实例检查中删除instance == null的检查'修复'了此问题(即,如果MySQLiteOpenHelper每次重新创建,则Robolectric运行测试时没有问题)。
public static synchronized MyDataManager getInstance(Context context){
if (sInstance == null) {
sInstance = new MyDataManager(context.getApplicationContext());
}
return sInstance;
}
private MyDataManager(Context context) {
dbHelper = new MySQLiteOpenHelper(context);
}
MySQLiteOpenHelper是SQLiteOpenHelper的一个简单扩展。
故障发生在(再次提醒,这是在我的数据库类内部):
database = dbHelper.getWritableDatabase();
很显然,我不想在我的应用程序中每次重新创建连接 - 我想没有人会希望这样做?这使我认为应该有一种正确的方法在Robolectric中做到这一点,只是我漏了什么技巧?
编辑:
此外,测试在隔离状态下成功运行,这使我认为它与Robolectric在测试用例之间移动并重用数据库连接有关?
两个测试都没有针对数据库或任何DB类执行具体操作。第一个测试启动一个片段,该片段将访问数据库以写入一些数据。第二个测试在尝试打开上述数据库时失败。
context.getApplicationContext()
- vkislicinsMyDataManagerTest
)? - Jared Burrows