Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing room migration, Cannot find the schema file in the assets folder [room-migration]

This issue came after I decided to add another entity to the room database. The schema is being exported in the expected directory. All build.gradle setting is done and seems to be working but is not. Since I got:

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/training/data-storage/room/migrating-db-versions#export-schema for details. Missing file: com.company.companyapp.db.AppStore/1.json

In fact both json schemas are being generated but the test runner is not able to find such files. Here is the gradle setup:

testOptions.unitTests.includeAndroidResources = true

defaultConfig {
    ...
    multiDexEnabled true
    javaCompileOptions {
        annotationProcessorOptions {
            arguments = ["room.schemaLocation": "$projectDir/store".toString()]
        }
    }

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    android.compileOptions.sourceCompatibility 1.8
    android.compileOptions.targetCompatibility 1.8
}

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

dependencies {
    def roomVersion = '2.2.1'
    // Other deps ...
    implementation "androidx.room:room-runtime:$roomVersion"
    implementation "androidx.room:room-rxjava2:$roomVersion"
    annotationProcessor "androidx.room:room-compiler:$roomVersion"
    testImplementation "androidx.room:room-testing:$roomVersion"

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation "androidx.test:core:$testCoreVersion"
    androidTestImplementation "androidx.test.ext:junit:$extJUnitVersion"
    testImplementation "androidx.test:core:$testCoreVersion"
    testImplementation "androidx.test.ext:junit:$extJUnitVersion"
    testImplementation "org.robolectric:robolectric:4.2.1"
}

I also note the Studio IDE marks the json schema directory holder enter image description here

By looking at the google room migration examples I can see there is a difference, and not talking about the name.
enter image description here

So it's very clear the gradle plugin is doing something different than I spect or the documentation says it should work, but it doesn't.

at androidx.room.testing.MigrationTestHelper.loadSchema(MigrationTestHelper.java:320)
at androidx.room.testing.MigrationTestHelper.createDatabase(MigrationTestHelper.java:152)
at com.company.companyapp.MigrationTest.migrate1To2(MigrationTest.java:32)
like image 492
AXSM Avatar asked Nov 01 '19 18:11

AXSM


People also ask

What is export schema in Room Android?

Export schemasThe exported JSON files represent your database's schema history. You should store these files in your version control system, as it allows Room to create older versions of the database for testing purposes.

What is fallbackToDestructiveMigration()?

fallbackToDestructiveMigration() Allows Room to destructively recreate database tables if Migration s that would migrate old database schemas to the latest schema version are not found.


2 Answers

Using this workaround found by PaulWoitaschek works for me with Robolectric 4.3:

add schemas dir on debug souceSets on your app's build.gradle

Groovy

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

Kotlin DSL

    sourceSets {
        getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
    }
like image 104
jyodoid Avatar answered Sep 27 '22 23:09

jyodoid


Please also make sure while creating the MigrationTestHelper to insert the actual database.class. I got confused and set my Entity.class instead, leading to the same error.

like image 36
LM_IT Avatar answered Sep 27 '22 21:09

LM_IT