Zookey
Zookey

Reputation: 2707

Android Room can not create JSON schema for testing migrations

I have created migration from 1 to 2 version of my database.

I have the app in a few modules like:

I have tried adding this into build.gradle of app and data modules:

javaCompileOptions {
        annotationProcessorOptions {
            arguments = ["room.schemaLocation":  "$projectDir/schemas".toString()]
        }
    }

    sourceSets {
        androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
    }

Here is my MigrationTest class:

@RunWith(AndroidJUnit4.class)
public class MigrationTest {

private static final String TEST_DB = "migration-test";

@Rule public MigrationTestHelper helper;

private Profile profile;

public MigrationTest() {
helper = new MigrationTestHelper(
    InstrumentationRegistry.getInstrumentation(),
    AppDatabase.class.getCanonicalName(),
    new FrameworkSQLiteOpenHelperFactory());
}

@Before
public void setUp(){
profile = createProfile();
}

@Test public void migrate1To2() throws IOException {
    SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);
    insertProfile(db);
    db.close();
    AppDatabase database = (AppDatabase) helper.runMigrationsAndValidate(TEST_DB, 2, true, MIGRATION_1_2);
    Single<ProfileData> profileDataSingle = database.profileDao().getById("userId");
    ProfileData profileData = profileDataSingle.blockingGet();
    Profile currentProfile = ProfileMapper.transform(profileData);
    assertEquals(currentProfile.getUserId(), profile.getUserId());
}

Here is failing test:

java.io.FileNotFoundException: Cannot find the schema file in the assets folder. Make sure to include the exported json schemas in your test assert inputs. See https://developer.android.com/topic/libraries/architecture/room.html#db-migration-testing for details. Missing file: org.app.app.data.sql.AppDatabase/1.json

Upvotes: 13

Views: 9702

Answers (5)

hannes ach
hannes ach

Reputation: 17743

For me it helps, when I add this (I simply forgot it). Maybe it helps someone

sourceSets {
    androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}

Upvotes: 5

Helton Malambane
Helton Malambane

Reputation: 1185

Happens because the json schema for the migration version is not found in schemas folder.

For instance, if you are testing migration from version 1 to 2, the file /schemas/*/1.json must exist.

Upvotes: 0

Sam
Sam

Reputation: 466

This solution in Kotlin is: Add the following to build.gradle(app):

android {
defaultConfig {
   kapt {
        arguments {
            arg("room.schemaLocation", "$projectDir/schemas")
        }
    }
}

sourceSets {
    getByName("androidTest") {
        assets.srcDirs(files(projectDir, "schemas"))
    }
}

dependencies {
   implementation 'androidx.room:room-runtime:2.2.3'
   kapt 'androidx.room:room-compiler:2.2.3'
   annotationProcessor 'androidx.room:room-compiler:2.2.3'
   implementation 'androidx.room:room-rxjava2:2.2.3'
   androidTestImplementation 'androidx.room:room-testing:2.2.3'
}

P.S: Dont Forget to set exportSchema = true in the Database file.

Upvotes: 2

Carson Holzheimer
Carson Holzheimer

Reputation: 2963

I had a similar problem to Zookey. All I was doing wrong was looking for a schema file that didn't exist. I was testing migration 8 to 9, but version 8.json wasn't generated. I had to created my database from version 9.json instead, and test from there.

Upvotes: 0

Evgenii Vorobei
Evgenii Vorobei

Reputation: 1569

For Kotliners:

android{
    defaultConfig {
        // ...
        kapt {
            arguments {
                arg("room.schemaLocation", "$projectDir/schemas")
            }
        }
    }
    sourceSets {
        getByName("androidTest"){
            assets.srcDirs(File(projectDir, "schemas"))
        }
    }
}

Upvotes: 7

Related Questions