Android Room无法创建用于测试迁移的JSON模式 [英] Android Room can not create JSON schema for testing migrations

查看:177
本文介绍了Android Room无法创建用于测试迁移的JSON模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了从1到2版本的数据库迁移.

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

我在以下几个模块中安装了该应用程序:

I have the app in a few modules like:

  • 应用
  • 数据

我尝试将其添加到应用程序和数据模块的build.gradle中:

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())
    }

这是我的MigrationTest类:

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());
}

这里测试失败:

java.io.FileNotFoundException:在 资产文件夹.确保将导出的json模式包括在您的 测试断言输入.看 https://developer.android.com/topic/library/architecture/room.html#db-migration-testing 有关详细信息.缺少文件:org.app.app.data.sql.AppDatabase/1.json

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

推荐答案

对于Kotliners:

For Kotliners:

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

这篇关于Android Room无法创建用于测试迁移的JSON模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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