Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Room MigrationTestHelper is deprecated

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()
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading