vki*_*ins 4 android robolectric android-sqlite
我有一个测试创建一个活动,试图从数据库中获取一些数据.这与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)
Run Code Online (Sandbox Code Playgroud)
在将数据库类移动到单例模型之前,这曾经工作正常.有任何建议如何处理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);
}
Run Code Online (Sandbox Code Playgroud)
MySQLiteOpenHelper是SQLiteOpenHelper的简单扩展.
失败发生在(再次,这是由db类内部):
database = dbHelper.getWritableDatabase();
Run Code Online (Sandbox Code Playgroud)
显然我不想每次都在我的应用程序中重新创建连接 - 而且我认为没有人会想要这样做?这让我觉得应该有一种方法在Robolectric中正确地做到这一点,我只是错过了一个技巧?
编辑:
此外,测试成功运行,这使我认为这与Robolectric在测试用例之间移动和重用数据库连接有关吗?
两个测试都不会对数据库执行任何特定的操作或与任何数据库类相关.第一次测试启动一个片段,该片段将访问数据库以写入一些数据.如上所述,第二次测试未能尝试打开数据库.
在每次测试之间重置所有单例实例,否则会产生类似你的副作用.
@After
public void finishComponentTesting() {
resetSingleton(YourSQLiteOpenHelper.class, "sInstance");
}
private void resetSingleton(Class clazz, String fieldName) {
Field instance;
try {
instance = clazz.getDeclaredField(fieldName);
instance.setAccessible(true);
instance.set(null, null);
} catch (Exception e) {
throw new RuntimeException();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2155 次 |
| 最近记录: |