MigrationTestHelper

public class MigrationTestHelper
extends TestWatcher

java.lang.Object
   ↳ org.junit.rules.TestWatcher
     ↳ androidx.room.testing.MigrationTestHelper


A class that can be used in your Instrumentation tests that can create the database in an older schema.

You must copy the schema json files (created by passing room.schemaLocation argument into the annotation processor) into your test assets and pass in the path for that folder into the constructor. This class will read the folder and extract the schemas from there.

 android {
   defaultConfig {
     javaCompileOptions {
       annotationProcessorOptions {
         arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
       }
     }
   }
   sourceSets {
     androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
   }
 }
 

Summary

Public constructors

MigrationTestHelper(Instrumentation instrumentation, String assetsFolder)

This constructor is deprecated. Cannot be used to run migration tests involving AutoMigration.

To test AutoMigration, you must use MigrationTestHelper(Instrumentation, Class, List, SupportSQLiteOpenHelper.Factory) for tests containing a ProvidedAutoMigrationSpec, or use MigrationTestHelper(Instrumentation, Class, List) otherwise.

MigrationTestHelper(Instrumentation instrumentation, String assetsFolder, SupportSQLiteOpenHelper.Factory openFactory)

This constructor is deprecated. Cannot be used to run migration tests involving AutoMigration.

To test AutoMigration, you must use MigrationTestHelper(Instrumentation, Class, List, SupportSQLiteOpenHelper.Factory) for tests containing a ProvidedAutoMigrationSpec, or use MigrationTestHelper(Instrumentation, Class, List) otherwise.

MigrationTestHelper(Instrumentation instrumentation, Class<? extends RoomDatabase> databaseClass)

Creates a new migration helper.

MigrationTestHelper(Instrumentation instrumentation, Class<? extends RoomDatabase> databaseClass, List<AutoMigrationSpec> specs)

Creates a new migration helper.

MigrationTestHelper(Instrumentation instrumentation, Class<? extends RoomDatabase> databaseClass, List<AutoMigrationSpec> specs, SupportSQLiteOpenHelper.Factory openFactory)

Creates a new migration helper.

Public methods

void closeWhenFinished(RoomDatabase db)

Registers a database connection to be automatically closed when the test finishes.

void closeWhenFinished(SupportSQLiteDatabase db)

Registers a database connection to be automatically closed when the test finishes.

SupportSQLiteDatabase createDatabase(String name, int version)

Creates the database in the given version.

SupportSQLiteDatabase runMigrationsAndValidate(String name, int version, boolean validateDroppedTables, Migration... migrations)

Runs the given set of migrations on the provided database.

Protected methods

void finished(Description description)
void starting(Description description)

Inherited methods

Public constructors

MigrationTestHelper

public MigrationTestHelper (Instrumentation instrumentation, 
                String assetsFolder)

This constructor is deprecated.
Cannot be used to run migration tests involving AutoMigration.

To test