开发者

cannot open database in android test app

开发者 https://www.devze.com 2023-02-13 09:00 出处:网络
I am trying to test the insertion and retrieval methods for my SQLiteOpenHelper subclass in an Android application.The SQLLiteHelper subclass exists in the app under test, and creates a database in th

I am trying to test the insertion and retrieval methods for my SQLiteOpenHelper subclass in an Android application. The SQLLiteHelper subclass exists in the app under test, and creates a database in the installation folder. However, the unit test exists in a InstrumentTestCase in the test app, and I would like to create a test database in the test app.

Unfortunately, if I try to create / open a database in the test app, I get the following exception:

android.database.sqlite.SQLiteException: unable to open database file
at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1584)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:638)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:168)
at com.kizoom.android.mybus.storage.MyStopsDatabase.getMyStops(MyStopsDatabase.java:63)
at com.kizoom.mybus.test.MyStopsDatabaseTest.testGetMyStops(MyStopsDatabaseTest.java:24)
at java.lang.reflect.Method.invokeNativ开发者_如何学Ce(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:191)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:181)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:164)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:151)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:425)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1520)

The following information appears in LogCat.

02-21 11:52:16.204: ERROR/Database(1454): sqlite3_open_v2("/data/data/com.kizoom.mybus.test/databases/MyStops", &handle, 6, NULL) failed

02-21 11:52:16.204: ERROR/SQLiteOpenHelper(1454): Couldn't open MyStops for writing (will try read-only):

02-21 11:52:16.204: ERROR/SQLiteOpenHelper(1454): android.database.sqlite.SQLiteException: unable to open database file

02-21 11:52:16.204: ERROR/SQLiteOpenHelper(1454):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)

02-21 11:52:16.204: ERROR/SQLiteOpenHelper(1454):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1584)

02-21 11:52:16.204: ERROR/SQLiteOpenHelper(1454):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:638)

Does anybody know why this would fail?


Instead of using getInstrumentation().getContext() for the context during create of the db helper, I changed it to getInstrumentation().getTargetContext() and it works.


I use the following setUp()-Method in my TestDatabaseHelper*-TestCases:

@Override
    protected void setUp() throws Exception {
        super.setUp();
        final SQLiteDatabase db = SQLiteDatabase.create(null);
        Context context = new MockContext() {
            @Override
            public SQLiteDatabase openOrCreateDatabase(String file, int mode, SQLiteDatabase.CursorFactory factory) {
                return db;
            };
        };
        mHelper = new MyCustomSubclassOfDatabaseHelper(context);
        mDb = mHelper.getWritableDatabase();
        wipeData(mDb);
    }
    public void wipeData(SQLiteDatabase db) {
        db.execSQL("DELETE FROM " + TABLENAME + ";");
    }

It's pretty cool because documentation says about SQLiteDatabase.create():

Create a memory backed SQLite database. Its contents will be destroyed when the database is closed.

So your testdatabase is not persisted and you can easily test your custom insert/update/query/delete methods.

Hope this helps. If there are any questions left, feel free to ask.

Cheers, Christoph

0

精彩评论

暂无评论...
验证码 换一张
取 消