测试房间迁移,在assets文件夹中找不到schema文件[room-migration] [英] Testing room migration, Cannot find the schema file in the assets folder [room-migration]

查看:29
本文介绍了测试房间迁移,在assets文件夹中找不到schema文件[room-migration]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是在我决定将另一个实体添加到房间数据库之后出现的.架构正在预期的目录中导出.所有 build.gradle 设置都已完成并且似乎正在工作但没有.因为我得到了:

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

实际上正在生成两个 json 模式,但测试运行程序无法找到此类文件.这是gradle设置:

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"
}

我还注意到 Studio IDE 标记了 json 架构目录持有者

I also note the Studio IDE marks the json schema directory holder

通过查看 google 房间迁移示例,我可以看到存在差异,而不是谈论名称.

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

所以很明显 gradle 插件做的事情与我想象的不同,或者文档说它应该可以工作,但它没有.

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)

推荐答案

使用由 PaulWoitaschek 使用 Robolectric 4.3 为我工作:

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

在应用程序的 build.gradle

常规

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

科特林 DSL

    sourceSets {
        getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
    }

这篇关于测试房间迁移,在assets文件夹中找不到schema文件[room-migration]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆