首页 > 解决方案 > 房间迁移:测试仪器过程崩溃

问题描述

问题背景:测试房间迁移时崩溃 -

测试仪器过程崩溃。检查 MigrationTest#migrationFrom5To6_containsCorrectData.txt 了解详细信息。

来自MigrationTest.java的样本

试验- 没有任何运气,但这是我迄今为止尝试过的。

文本文件详细信息

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;
    }
}

标签: androidandroid-room

解决方案


推荐阅读