Issue
I know Robolectric is not using the real SQLite database, that it uses in memory database. But the database is recreated every time getWritableDatabase() or getWritableDatabase() is called.
Is there a way to make Robolectric recreate the database let's say only when setUp() is called or something like that?
Solution
I downloaded the Robolectric source from github and made the changes explained in the answer to this question:
Testing SQLite database in Robolectric
In the ShadowSQLiteOpenHelper class I made these changes:
Defined this field:
private static Context previousContext;In the constructor added this:
if (previousContext == null) { previousContext = context; } else { if(previousContext == context) { return; } else { previousContext = context; } }Changed the
getDatabase()method like this:private SQLiteDatabase getDatabase() { if (database == null) { database = SQLiteDatabase.openDatabase("path", null, 0); realHelper.onCreate(database); } realHelper.onOpen(database); return database; }
And now everything works perfect.
Answered By - nikmin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.