Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot open database in android test app

Tags:

android

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?

like image 760
Dave Springgay Avatar asked Feb 23 '11 08:02

Dave Springgay


1 Answers

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

like image 180
Aviral Avatar answered Oct 11 '22 19:10

Aviral