本文介绍了Robolectric访问数据库抛出一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建它试图从数据库中获取一些数据的活动测试。这种失败SQLiteException

I have a test that creates an activity which attempts to get some data from the database. This fails with 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处理?我找不到这个任何文件或样品。

This used to work fine before I moved my database class to a singleton model. Any suggestions how this should be handled with Robolectric? I couldn't find any documentation or samples on this.

编辑:

运行Robolectric 3.0 RC-2

Running Robolectric 3.0 RC-2

Robolectric是驾驶我的活动,它试图做一些工作与数据库。
在我的应用程序DB类,自下而上修复的问题,比如删除复== NULL(即有与Robolectric运行测试,如果MySQLiteOpenHelper重新创建每次都没有问题)

Robolectric is driving my activity which attempts to do some work with the database. In my app DB class, removing the check for instance == null from below 'fixes' the issue (i.e. there's no issue with Robolectric running the test if MySQLiteOpenHelper is recreated every time)

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的简单扩展。

MySQLiteOpenHelper is a simple extension of SQLiteOpenHelper.

该故障发生在(再一次,这里面由DB类):

The failure is happening on (again, this is inside by db class):

        database = dbHelper.getWritableDatabase();

很显然,我真的不希望我的应用程序每次都重新创建一个连接 - 我想没有人会希望出现这种情况?这使我觉得应该有一个办法做到这一点正确地Robolectric,我只是缺少一个把戏吗?

Obviously I don't really want to recreate a connection every time in my App - and I think nobody would want that? Which makes me think there should be a way to do this properly in Robolectric and I'm just missing a trick here?

编辑:

此外,测试运行成功孤立的,这让我觉得这件事情做Robolectric测试用例之间移动和重复使用的数据库连接?

Also, the test runs successfully in isolation, which makes me think it's something to do with Robolectric moving between the test cases and reusing the database connection?

这两项测试没有做具体的数据库或相关的任何数据库类的任何东西。第一次测试开始,这将访问数据库编写一些数据片段。第二次测试失败在试图打开数据库同上。

Both tests don't do ANYTHING specific to database or related to any of the DB classes. First test starts a fragment which will access the database to write some data. Second test is failing on attempt to open the db as above.

推荐答案

全部重设单实例每次测试之间,或者你会得到副作用,像你这样的。

Reset all singleton instances between each test or you will get side effects like yours.

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

在https://github.com/nenick/QuAcc/blob/master/AppCT/src/test/java/de/nenick/robolectric/RoboComponentTestBase.java

这篇关于Robolectric访问数据库抛出一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:19