J'ai lu Room Persistence Library. Je clone aussi android-architecture-components puis j'essaye d'ajouter le test de Mirgration. Cependant, je ne peux pas importerImpossible d'importer android.arch.persistence.room.testing.MigrationTestHelper
import android.arch.persistence.room.testing.MigrationTestHelper;
J'utilise aussi la dernière version de lib qui est.
android.arch.core:core-testing:1.0.0-alpha3
Voici le code pour 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.
}
}
Merci. Pouvons-nous exécuter ce code en tant que test d'unité locale sur la machine hôte? – UmAnusorn
Peut-il fonctionner en utilisant testCompile .. sur un test d'unité locale? – UmAnusorn
Je pense qu'il y a une erreur depuis que leur exemple de code utilise AndroidJUnit4 – UmAnusorn