È possibile creare un caso di test che costruisca una vecchia versione del database e quindi esegua la migrazione. Ecco un esempio ridotto.
public class DbHelperTest extends AndroidTestCase {
private SQLiteDatabase db;
@Override
public void setUp() throws Exception {
super.setUp();
mContext = new RenamingDelegatingContext(getContext(), "db_helper_test_");
SQLiteDatabase.CursorFactory cursorFactory = new SQLiteDatabase.CursorFactory() {
@Override
public Cursor newCursor(final SQLiteDatabase db, final SQLiteCursorDriver masterQuery, final String editTable,
final SQLiteQuery query) {
return new SQLiteCursor(db, masterQuery, editTable, query);
}
};
db = SQLiteDatabase.create(cursorFactory);
createV14Db(db);
}
@Override
public void tearDown() throws Exception {
super.tearDown();
db.close();
}
private void createV14Db(SQLiteDatabase db) {
// Create tables and indices
db.execSQL(...);
// Create some data
db.execSQL(...);
}
public void testDbUpgrade() {
DbHelper dbHelper = new DbHelper(mContext);
dbHelper.onUpgrade(db, 14, 100);
// Check that things have been upgraded correctly
}
}