I just created unit tests for my CRUD implementations and I have this weird problem. When tests are run one by one, they all pass. But when I want to run the whole suite, they fail with the error
android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471)
at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1367)
at cz.pikadorama.simpleorm.DatabaseSanityTest.testDelete(DatabaseSanityTest.java:55)
Here's the code
@Before
public void prepareDatabase() throws InstantiationException, IllegalAccessException {
Context context = InstrumentationRegistry.getTargetContext();
context.deleteDatabase(DATABASE_NAME);
DbManager.registerHelper(new TestSQLiteHelper(context), new Class<?>[]{TestEntity.class});
}
@Test
public void testDelete() {
Dao<TestEntity> dao = DaoManager.getDao(TestEntity.class);
TestEntity entity = new TestEntity();
dao.create(entity);
dao.delete(entity);
assertEquals(0, dao.findAll().size());
}
// other tests for insert, update, ... follow
By the way, DAO implementations always open writable database as sqliteOpenHelper.getWritableDatabase().
@Beforemethod into@BeforeClassmethod. Does it resolve the issue?@BeforeClassand cleaned the database instead of deleting it before each test.