Room Migrations — “Testing”

android {
...
kapt {
arguments {
arg("room.schemaLocation", "$projectDir/schemas")
}
}
sourceSets {
// Adds exported schema location as test app assets.
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
}

dependencies {
...
androidTestImplementation "androidx.room:room-testing:2.4.0"
}
// put this inside the app's build.gradle
@RunWith(AndroidJUnit4::class)
class MigrationTest {
private val TEST_DB = "migration-test"

@get:Rule
val helper: MigrationTestHelper = MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
MigrationDb::class.java.canonicalName,
FrameworkSQLiteOpenHelperFactory()
)

@Test
@Throws(IOException::class)
fun migrate1To2() {
var db = helper.createDatabase(TEST_DB, 1).apply {
// db has schema version 1. insert some data using SQL queries.
// You cannot use DAO classes because they expect the latest schema.
execSQL(...)

// Prepare for the next version.
close()
}

// Re-open the database with version 2 and provide
// MIGRATION_1_2 as the migration process.
db = helper.runMigrationsAndValidate(TEST_DB, 2, true, MIGRATION_1_2)

// MigrationTestHelper automatically verifies the schema changes,
// but you need to validate that the data was migrated properly.
}
}
@RunWith(AndroidJUnit4::class)
class MigrationTest {
private val TEST_DB = "migration-test"

// Array of all migrations
private val ALL_MIGRATIONS = arrayOf(
MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4)

@get:Rule
val helper: MigrationTestHelper = MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
AppDatabase::class.java.canonicalName,
FrameworkSQLiteOpenHelperFactory()
)

@Test
@Throws(IOException::class)
fun migrateAll() {
// Create earliest version of the database.
helper.createDatabase(TEST_DB, 1).apply {
close()
}

// Open latest version of the database. Room will validate the schema
// once all migrations execute.
Room.databaseBuilder(
InstrumentationRegistry.getInstrumentation().targetContext,
AppDatabase::class.java,
TEST_DB
).addMigrations(*ALL_MIGRATIONS).build().apply {
openHelper.writableDatabase.close()
}
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Setup for your Automation Testing in your Windows Computer

The Wrong, Hacked, and Correct way of Android Deep Linking for Redirected MultiSite with AutoVerify

Use of WorkManager in Android App (Part-3)

Transforming picture with Android Canvas’ DrawBitmapMesh

How Can I Quickly Integrate AppGallery Connect Cloud DB into a Unity App…

Building Android Widgets In Flutter: Remember these Simple Points

Tricky refactoring of Jetpack Compose code — be careful with side effects

Dart Excitement From Kotliner Perspective

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nataraj KR

Nataraj KR

More from Medium

Android deep dive: findViewById

Room Database in Kotlin

Exo-Player — The DRM Way(Part III)

Android Adventure