Skip to content

Instantly share code, notes, and snippets.

@singhangadin
Created September 29, 2019 20:55
Show Gist options
  • Save singhangadin/8751e6eb19e8731ade3ee8ea2a6231cf to your computer and use it in GitHub Desktop.
Save singhangadin/8751e6eb19e8731ade3ee8ea2a6231cf to your computer and use it in GitHub Desktop.
Testing Room Migrations
@RunWith(AndroidJUnit4.class)
public class DatabaseInstrumentedTest {
private static final String TEST_DB_NAME = "test_database";
@Rule
public MigrationTestHelper testHelper =
new MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
TestDatabase.class.getCanonicalName(),
new FrameworkSQLiteOpenHelperFactory()
);
@Test
public void testAllMigrations() throws IOException {
testHelper.createDatabase(TEST_DB_NAME, 1);
testHelper.runMigrationsAndValidate(TEST_DB_NAME, Constants.DB_VERSION, true, migrations);
}
}
public class DatabaseModule {
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("DROP TABLE IF EXISTS dummy_table");
}
};
@NonNull
public static Migration[] migrations = {
MIGRATION_1_2
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment