android - 房间迁移:测试仪器过程崩溃
问题描述
问题背景:测试房间迁移时崩溃 -
测试仪器过程崩溃。检查 MigrationTest#migrationFrom5To6_containsCorrectData.txt 了解详细信息。
试验- 没有任何运气,但这是我迄今为止尝试过的。
调试时,测试执行过程将永远进行。
如果我只是尝试将几个打印语句放在
migrationFrom5To6_containsCorrectData
任何内容中都不会打印。所以我的盲目猜测是设置过程中有问题,即getMigratedRoomDatabase
.
文本文件详细信息
INSTRUMENTATION_RESULT: shortMsg=Process crashed.
INSTRUMENTATION_CODE: 0
房间版本: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;
}
}
解决方案
推荐阅读
- android - 如何将 NFC 标签传递给其他 Activity
- linux - 在 bash 脚本中自动向 ftp 提供命令
- alexa - Alexa AMAZON.City 插槽类型识别小村庄而不是大城市
- java - java.lang.IllegalArgumentException:尝试删除从未添加的 OnFrameMetricsAvailableListener
- angular - 离子警报并将值传递给 handler()
- python - 如何在 python 中为 150 万用户创建友谊矩阵?
- c# - 指定的强制转换在 FreshMvvm Xamarin 中是无效的异常
- excel - 使用 VBA 在 Excel 中搜索特定单词的数据,将该行和上下 2 行复制到新工作表中
- javascript - 如何干净地猴子修补 Javascript 中的函数?
- angular - 如何检索firebase时间戳