Home > OS >  Room MigrationTestHelper is deprecated
Room MigrationTestHelper is deprecated

Time:01-21

I want to write a JUnit test for my database migrations like as android documentation says, but in room version 2.4.1 MigrationTestHelper is deprecated, is there any alternative for this to test my legacy code that used manual migration?

@RunWith(AndroidJUnit4::class)
class MigrationTest {
    private val TEST_DB = "migration-test"

    // Array of all migrations
    private val ALL_MIGRATIONS = arrayOf(
        PersistenceModule.MIGRATION_7_8,
        PersistenceModule.MIGRATION_8_9,
        PersistenceModule.MIGRATION_9_10,
        PersistenceModule.MIGRATION_10_11,
        PersistenceModule.MIGRATION_11_12,
        PersistenceModule.MIGRATION_12_13,
        PersistenceModule.MIGRATION_13_14
    )

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

    @Test
    @Throws(IOException::class)
    fun migrateAll() {
       
        helper.createDatabase(TEST_DB, 7).apply {
            close()
        }

        Room.databaseBuilder(
            InstrumentationRegistry.getInstrumentation().targetContext,
            AppDatabase::class.java,
            TEST_DB
        ).addMigrations(*ALL_MIGRATIONS).build().apply {
            openHelper.writableDatabase.close()
        }
    }
}

CodePudding user response:

That's simply not true; only 2 of the 5 constructors had been deprecated:

https://developer.android.com/reference/androidx/room/testing/MigrationTestHelper

Most likely because:

Cannot be used to run migration tests involving AutoMigration.

  •  Tags:  
  • Related