MigrationTestHelper
open class MigrationTestHelper : 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 | |
---|---|
<init>(instrumentation: Instrumentation!, assetsFolder: String!) Creates a new migration helper. |
|
<init>(instrumentation: Instrumentation!, assetsFolder: String!, openFactory: SupportSQLiteOpenHelper.Factory!) Creates a new migration helper. |
Public methods | |
---|---|
open Unit |
Registers a database connection to be automatically closed when the test finishes. |
open Unit |
Registers a database connection to be automatically closed when the test finishes. |
open SupportSQLiteDatabase! |
createDatabase(name: String!, version: Int) Creates the database in the given version. |
open SupportSQLiteDatabase! |
runMigrationsAndValidate(name: String!, version: Int, validateDroppedTables: Boolean, vararg migrations: Migration!) Runs the given set of migrations on the provided database. |
Protected methods | |
---|---|
open Unit |
finished(description: Description!) |
open Unit |
starting(description: Description!) |
Public constructors
<init>
MigrationTestHelper(
instrumentation: Instrumentation!,
assetsFolder: String!)
Creates a new migration helper. It uses the Instrumentation context to load the schema (falls back to the app resources) and the target context to create the database.
Parameters | |
---|---|
instrumentation |
Instrumentation!: The instrumentation instance. |
assetsFolder |
String!: The asset folder in the assets directory. |
<init>
MigrationTestHelper(
instrumentation: Instrumentation!,
assetsFolder: String!,
openFactory: SupportSQLiteOpenHelper.Factory!)
Creates a new migration helper. It uses the Instrumentation context to load the schema (falls back to the app resources) and the target context to create the database.
Parameters | |
---|---|
instrumentation |
Instrumentation!: The instrumentation instance. |
assetsFolder |
String!: The asset folder in the assets directory. |
openFactory |
SupportSQLiteOpenHelper.Factory!: Factory class that allows creation of SupportSQLiteOpenHelper |
Public methods
closeWhenFinished
open fun closeWhenFinished(db: SupportSQLiteDatabase!): Unit
Registers a database connection to be automatically closed when the test finishes.
This only works if MigrationTestHelper
is registered as a Junit test rule via Rule
annotation.
Parameters | |
---|---|
db |
SupportSQLiteDatabase!: The database connection that should be closed after the test finishes. |
closeWhenFinished
open fun closeWhenFinished(db: RoomDatabase!): Unit
Registers a database connection to be automatically closed when the test finishes.
This only works if MigrationTestHelper
is registered as a Junit test rule via Rule
annotation.
Parameters | |
---|---|
db |
RoomDatabase!: The RoomDatabase instance which holds the database. |
createDatabase
open fun createDatabase(
name: String!,
version: Int
): SupportSQLiteDatabase!
Creates the database in the given version. If the database file already exists, it tries to delete it first. If delete fails, throws an exception.
Parameters | |
---|---|
name |
String!: The name of the database. |
version |
Int: The version in which the database should be created. |
Return | |
---|---|
SupportSQLiteDatabase! |
A database connection which has the schema in the requested version. |
Exceptions | |
---|---|
IOException |
If it cannot find the schema description in the assets folder. |
runMigrationsAndValidate
open fun