Search code examples
androiddatabase-migrationandroid-roomandroid-architecture-components

Cannot import android.arch.persistence.room.testing.MigrationTestHelper


I have read Room Persistence Library. I also clone android-architecture-components then I try to add Mirgration test. However, I cannot import

import android.arch.persistence.room.testing.MigrationTestHelper;

I also use latest lib version which is.

android.arch.core:core-testing:1.0.0-alpha3

Here is the code for MigrationTest

import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import android.arch.persistence.room.testing.MigrationTestHelper;

@RunWith(AndroidJUnit4.class)
public class MigrationTest {
    private static final String TEST_DB = "migration-test";

    @Rule
    public MigrationTestHelper helper;

    public MigrationTest() {
        helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
                MigrationDb.class.getCanonicalName(),
                new FrameworkSQLiteOpenHelperFactory());
    }

    @Test
    public void migrate1To2() throws IOException {
        SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);

        // db has schema version 1. insert some data using SQL queries.
        // You cannot use DAO classes because they expect the latest schema.
        //db.execSQL(...);

        // Prepare for the next version.
        db.close();

        // Re-open the database with version 2 and provide
        // MIGRATION_1_2 as the migration process.
        db = helper.runMigrationsAndValidate(TEST_DB, 2, true, MIGRATION_1_2);

        // MigrationTestHelper automatically verifies the schema changes,
        // but you need to validate that the data was migrated properly.
    }
}

Solution

  • Since the code use AndroidJUnit4 then Just use androidTestCompile instead

     androidTestCompile "android.arch.persistence.room:testing:$arch_version"
    

    Official doc use dependency for local unit test. However, the official sample use Android runner...

    https://developer.android.com/topic/libraries/architecture/adding-components.html