我有一个测试创建一个尝试从数据库获取一些数据的活动。失败并出现 SQLiteException
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 来处理吗?我找不到这方面的任何文档或示例。
EDIT:
运行 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 的简单扩展。
失败发生在(同样,这是在 db 类内部):
database = dbHelper.getWritableDatabase();
显然,我真的不想每次在我的应用程序中重新创建连接 - 我认为没有人会想要这样?这让我觉得应该有一种方法可以在 Robolectric 中正确地做到这一点,而我只是在这里错过了一个技巧?
EDIT:
另外,测试独立运行成功,这让我认为这与 Robolectric 在测试用例之间移动和重用数据库连接有关?
这两个测试都不执行任何特定于数据库或与任何数据库类相关的操作。第一个测试启动一个片段,它将访问数据库以写入一些数据。第二个测试在尝试打开数据库时失败,如上所述。