0

私はRoom Persistence Libraryを読みました。私もandroid-architecture-componentsを複製して、私はMirgrationテストを追加しようとします。ただし、インポートできません。android.arch.persistence.room.testing.MigrationTestHelperをインポートできません

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

最新のlibバージョンも使用しています。ここ

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

コード使用AndroidJUnit4ので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. 
    } 
} 

答えて

1

あなたは、Androidのランナー(AndroidJUnit4.class)を使用している、そして、あなたのテストをactualy src/androidTestに配置されます。それはあなたが依存関係を宣言しなければならないInstrumented Tests使用している意味:Googleのドキュメントで

// Local Unit Test 
testCompile .... 

src/testに配置されたコードをテストし、

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

一方、あなたはLocal Unit Testを書いている場合は、あなたは依存関係を宣言することができます彼らは地元の単体テストの例を挙げています。間違いなし。

+0

ありがとうございます。そのコードをホストマシン上のローカルユニットテストとして実行できますか? – UmAnusorn

+0

ローカルユニットテストでtestCompile ..を使用して実行できますか? – UmAnusorn

+0

サンプルコードはAndroidJUnit4を使用しているので間違いがあると思います – UmAnusorn

関連する問題