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.invokeNative(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With