Room迁移测试运行失败:提示找不到schema文件该如何解决?
Room迁移测试无法加载schema文件问题排查
问题背景
我正在为Room数据库编写Android迁移测试,按照官方步骤操作,已生成所需schema,也在Gradle构建文件中配置了assets路径,还添加了多个构建类型的相关配置,但运行测试时仍然提示无法加载schema文件,找不到问题所在。
- Room版本:2.3.0
报错信息
Cannot find the schema file in the assets folder. Make sure to include the exported json schemas in your test assert inputs. Missing file: Asset file database.Sauvegarde/1.json not found 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. Missing file: Asset file database.Sauvegarde/1.json not found at androidx.room.testing.MigrationTestHelper.loadSchema(MigrationTestHelper.java:326) at androidx.room.testing.MigrationTestHelper.createDatabase(MigrationTestHelper.java:152) at globalTests.migrations.MigrationTest.migrate1To2(MigrationTest.java:31) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575) at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:263) at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834)
相关配置与代码
测试代码
package globalTests.migrations; import ...; @RunWith(AndroidJUnit4.class) public class MigrationTest { private static final String TEST_DB = "migration-test"; @Rule public MigrationTestHelper helper; public MigrationTest() { helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(), Sauvegarde.class.getCanonicalName(), new FrameworkSQLiteOpenHelperFactory()); } @Test public void migrate1To2() throws IOException { SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1); // Prepare for the next version. db.close(); // Re-open the database with version 2 and provide // MIGRATION_1_2 as the migration process. db = helper.runMigrationsAndValidate(TEST_DB, 2, true, Migrations.MIGRATION_1_2); // MigrationTestHelper automatically verifies the schema changes, // but you need to validate that the data was migrated properly. } }
build.gradle配置
testOptions { execution 'ANDROIDX_TEST_ORCHESTRATOR' } javaCompileOptions { annotationProcessorOptions { arguments += ["room.schemaLocation": "$projectDir/schemas".toString()] } } sourceSets { // Adds exported schema location as test app assets. debug.assets.srcDirs += files("$projectDir/schemas".toString()) customDebugType.assets.srcDirs += files("$projectDir/schemas".toString()) androidTest.assets.srcDirs += files("$projectDir/schemas".toString()) test.assets.srcDirs += files("$projectDir/schemas".toString()) }
目录结构
测试结构:
schema存放位置:
解决方案
按以下步骤逐一排查即可解决:
- 确认生成的schema文件夹名与数据库类全类名完全匹配。报错提示需要的是
database.Sauvegarde/1.json,检查schemas目录下的文件夹名是否为database.Sauvegarde,如果数据库类包名有过变更,删除旧的schema文件夹后重新构建生成新的schema。 - 检查测试运行的构建变体,如果你配置了除debug、customDebugType之外的其他自定义构建变体,需要对应添加该变体的assets配置,也可以直接统一配置所有变体的assets路径:
sourceSets.all { sourceSet -> sourceSet.assets.srcDirs += files("$projectDir/schemas") }
- 如果你的测试放在
test目录(本地单元测试,不是androidTestinstrumentation测试),需要额外添加测试资源配置,确保schema被打包到测试资源中:
tasks.withType(Test) { resources.srcDirs += files("$projectDir/schemas") }
- 清理构建缓存后重新运行:依次执行
Build -> Clean Project、Rebuild Project,之后再启动测试。 - 若以上步骤都无效,可手动将
schemas目录下的所有文件复制到test/resources或者androidTest/assets目录,强制测试可读取到schema文件。
内容的提问来源于stack exchange,提问作者lupaulus
相关产品推荐
相关产品推荐

