Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
    }
}
like image 759
UmAnusorn Avatar asked Jun 26 '17 04:06

UmAnusorn


2 Answers

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

like image 182
UmAnusorn Avatar answered Oct 19 '22 04:10

UmAnusorn


You are using Android runner (AndroidJUnit4.class), and your test is actualy placed at src/androidTest. It means you are using Instrumented Tests which dependencies should be declared:

// Instrumented Unit Test or UI Test
androidTestComplile ....  

Meanwhile, if you are writing Local Unit Test, testing codes is placed at src/test, you can declare the dependencies:

// Local Unit Test
testCompile ....

In Google documentation, they just give an example for local unit tests. No mistake here.

like image 33
Quang Nguyen Avatar answered Oct 19 '22 03:10

Quang Nguyen