开发者

"Unable to open database file" when using SQLiteOpenHelper with instrumentation context

开发者 https://www.devze.com 2023-01-25 19:12 出处:网络
I\'m trying to create a database that contains mock objects for testing purposes.I\'d prefer to have this db reside in my test package instead of in my application package.

I'm trying to create a database that contains mock objects for testing purposes. I'd prefer to have this db reside in my test package instead of in my application package.

protected class MockObjectOpenHelper extends SQLiteOpenHelper {

    MockObjectOpenHelper() {
        super(instrumentation.getContext(), FILE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        ...
    }
}

When I create my SQLiteOpenHelper from within my app, I pass in the instrumentation context as well as a path that's relative to my test package's database directory. I would think this would allow me to create a database in my test package, but this doesn't seem to be the case. The "databases" directory is never created in my test package.

Is there a way to have my app create and access a database that resides in my test project?

The exception:

14:50:13.917 pool-1-thread-1 android.database.sqlite.SQLiteException: unable to open database file
at android.database.sqlite.SQLiteDatabase.dbopen(Nati开发者_如何学JAVAve Method)
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1584)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:638)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:659)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:652)
at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:482)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
at com.myapp.test.http.DbMockHttpClient.createClientRequestDirector(DbMockHttpClient.java:52)
at com.myapp.test.http.UniversalMockHttpClient.createClientRequestDirector(UniversalMockHttpClient.java:42)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:539)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
E


I wasn't sissified with @Yves solution because it results in databases created in tested appliction's folder. This can potentially interfere with the application (like purging databases during tests).

I've made some investigations. This error is thrown because "databases" directory fails to be created, and when SQL engine tries to open "databases/dbname.db" file it can't find "databases" folder.

Usually Context is responsible for creating "databases" folder just before creating database for the first time. But it fails because it has invalid permissions. And here is why.

Let's assume Tested project is installed with user app_100, and Tester project is installed with user app_101. When tests are running, the Instrumentation runs using app_100 user, and not app_101. And app_100 has not right to create folder in app_101 private folder.

The easiest way to solve this is to add same value to "android:sharedUserId" int Tester and Tested AndroidManifest.xml. This way both .apk will be installed with same user (in our example this would be app_100).


I had the same issue. Using the application context did the trick for me:

this.getActivity().getApplicationContext()

instead of

instrumentation.getContext()
0

精彩评论

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

关注公众号