问题背景:测试会议室迁移时崩溃-
测试工具进程崩溃。有关详细信息,请查看MigrationTest#migrationFrom5To6_containsCorrectData.txt。
的示例试用-没什么运气,但这是我到目前为止尝试过的。
调试时,测试执行过程将永远进行下去。
如果我只是尝试在migrationFrom5To6_containsCorrectData
中放入情侣打印语句,则不会打印任何内容。因此,我的盲目猜测是在设置过程getMigratedRoomDatabase
中出现了问题。
文本文件详细信息
INSTRUMENTATION_RESULT: shortMsg=Process crashed.
INSTRUMENTATION_CODE: 0
RoomVersion :1.1.1-rc1
测试类
@RunWith(AndroidJUnit4.class) public class MigrationTest {
@Rule public MigrationTestHelper testHelper =
new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
SampleDb.class.getCanonicalName(), new FrameworkSQLiteOpenHelperFactory());
private static final String TEST_DB_NAME = "test-db";
private static final User FIRST_USER = createDummyUser(true);
private static final User SECOND_USER = createDummyUser(false);
@Test public void migrationFrom5To6_containsCorrectData() throws Exception {
//Create the database in version 4
SupportSQLiteDatabase db = testHelper.createDatabase(TEST_DB_NAME, 5);
// Insert some data
insertUser(FIRST_USER, db);
insertUser(SECOND_USER, db);
//Prepare for the next version
db.close();
// Re-open the database with version 6 and provide MIGRATION_4_6
// and MIGRATION_5_6 as the migration process.
testHelper.runMigrationsAndValidate(TEST_DB_NAME, 6, true, MIGRATION_2_6, MIGRATION_4_6,
MIGRATION_5_6);
// MigrationTestHelper automatically verifies the schema
//changes, but not the data validity
// Validate that the data was migrated properly.
UserDao dbUser = getMigratedRoomDatabase().userDao();
// getUsers return Flowable<List<User>>
dbUser.getUsers().test().assertValueCount(2);
}
private SampleDb getMigratedRoomDatabase() {
SampleDb database =
Room.databaseBuilder(InstrumentationRegistry.getTargetContext(), SampleDb.class,
TEST_DB_NAME).addMigrations(MIGRATION_2_6, MIGRATION_4_6, MIGRATION_5_6).build();
// close the database and release any stream resources when the test finishes
testHelper.closeWhenFinished(database);
return database;
}
private void insertUser(User user, SupportSQLiteDatabase db) {
ContentValues values = new ContentValues();
values.put("userId", user.getUserId());
// Bunch of other user values
...
db.insert("user", SQLiteDatabase.CONFLICT_REPLACE, values);
}
private static User createDummyUser(boolean isFirst) {
User dummy = new User();
dummy.setData(2);
return dummy;
}
}