gpt4 book ai didi

android - Robolectric 访问数据库抛出错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:22 26 4
gpt4 key购买 nike

我有一个测试创建了一个试图从数据库中获取一些数据的 Activity 。这失败并出现 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 正在插入我的 Activity ,该 Activity 试图对数据库进行一些操作。在我的应用程序数据库类中,从下面“修复”问题中删除例如 == 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 在测试用例之间移动并重用数据库连接有关?

这两个测试都不做任何特定于数据库或与任何数据库类相关的事情。第一个测试启动一个 fragment ,它将访问数据库以写入一些数据。第二次测试未能如上所示尝试打开数据库。

最佳答案

在每次测试之间重置所有单例实例,否则您将得到像您一样的副作用。

@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();
}
}

关于android - Robolectric 访问数据库抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30308776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com