android – Robolectric访问数据库会引发错误

android – Robolectric访问数据库会引发错误,第1张

概述我有一个测试创建一个活动,试图从数据库中获取一些数据.这与SQLiteException失败 17:40:40.528 [DEBUG] [TestEventLogger] android.database.sqlite.SQLiteException: Cannot open SQLite connection, base error code: 1417:40:40.528 [DEBU 我有一个测试创建一个活动,试图从数据库中获取一些数据.这与sqliteException失败

17:40:40.528 [DEBUG] [TestEventLogger]     androID.database.sqlite.sqliteException: Cannot open sqlite connection,base error code: 1417: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.access0(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在测试用例之间移动和重用数据库连接有关吗?

两个测试都不会对数据库执行任何特定的 *** 作或与任何数据库类相关.第一次测试启动一个片段,该片段将访问数据库以写入一些数据.如上所述,第二次测试未能尝试打开数据库.

解决方法 在每次测试之间重置所有单例实例,否则会产生类似你的副作用.

@Afterpublic 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访问数据库会引发错误全部内容,希望文章能够帮你解决android – Robolectric访问数据库会引发错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1130152.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存