【发布时间】:2016-01-14 22:30:35
【问题描述】:
我有一个测试,它创建了一个尝试从数据库中获取一些数据的活动。这会因 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 处理?我找不到这方面的任何文档或示例。
编辑:
运行 Robolectric 3.0 RC-2
Robolectric 正在推动我尝试对数据库进行一些工作的活动。 在我的应用程序数据库类中,从“修复”问题下方删除实例 == 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 中正确地做到这一点,而我只是在这里错过了一个技巧?
编辑:
另外,测试在隔离中成功运行,这让我觉得这与 Robolectric 在测试用例之间移动和重用数据库连接有关?
这两个测试都不做任何特定于数据库或与任何 DB 类相关的事情。第一个测试启动一个片段,该片段将访问数据库以写入一些数据。第二次测试在尝试打开数据库时失败。
【问题讨论】:
-
向我们展示代码。什么版本的 robolectric?
-
哪个代码有帮助?在失败/通过运行之间我没有太多改变 - 只是将我的 DB 类包装到一个单例模型中
-
我看到你已经找到了:github.com/robolectric/robolectric/issues/1622。我总是要求提供代码以获得更好的理解。
-
@JaredBurrows 很公平。是的,很抱歉在这两个地方发帖-不确定这是一个错误还是我没有正确使用 Robolectric...如果有帮助,我已经添加了一些代码...
-
不,它真的没有,因为你隐瞒了很多。您是否在使用
application作为您的上下文?
标签: android android-sqlite robolectric