Room MigrationTestHelper 已弃用
Room MigrationTestHelper is deprecated
我想为我的数据库迁移编写一个 JUnit 测试,如 android documentation 所说,但是在房间版本 2.4.1 MigrationTestHelper 中已弃用,是否有任何替代方法可以测试我的使用手动迁移的遗留代码?
@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()
}
}
}
这根本不是真的; 5 个构造函数中只有 2 个被弃用:
https://developer.android.com/reference/androidx/room/testing/MigrationTestHelper
很可能是因为:
Cannot be used to run migration tests involving AutoMigration
.
我想为我的数据库迁移编写一个 JUnit 测试,如 android documentation 所说,但是在房间版本 2.4.1 MigrationTestHelper 中已弃用,是否有任何替代方法可以测试我的使用手动迁移的遗留代码?
@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()
}
}
}
这根本不是真的; 5 个构造函数中只有 2 个被弃用:
https://developer.android.com/reference/androidx/room/testing/MigrationTestHelper
很可能是因为:
Cannot be used to run migration tests involving
AutoMigration
.