gpt4 book ai didi

Android Room 无法创建用于测试迁移的 JSON 模式

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:00 28 4
gpt4 key购买 nike

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

我有几个模块中的应用程序,例如:

  • 应用
  • 数据
  • 域名

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

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

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

这是我的 MigrationTest 类:

@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: 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:

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

关于Android Room 无法创建用于测试迁移的 JSON 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51155165/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com