这个问题是在我决定将另一个实体添加到会议室数据库之后发生的。模式已导出到预期目录中。所有build.gradle设置都已完成,并且似乎可以正常运行,但没有成功。自从我得到:
private void HighlightText(object controlToHighlight, string textToHighlight)
{
if (controlToHighlight == null) return;
if (controlToHighlight is TextBlock tb)
{
var regex = new Regex("(" + textToHighlight + ")", RegexOptions.IgnoreCase);
if (textToHighlight.Length == 0)
{
var str = tb.Text;
tb.Inlines.Clear();
tb.Inlines.Add(str);
return;
}
var substrings = regex.Split(tb.Text);
tb.Inlines.Clear();
foreach (var item in substrings)
{
if (regex.Match(item).Success)
{
var run = new Run(item)
{
Background = (SolidColorBrush) new BrushConverter().ConvertFrom("#FFFFF45E")
};
tb.Inlines.Add(run);
}
else
{
tb.Inlines.Add(item);
}
}
}
else
{
if (!(controlToHighlight is DependencyObject dependencyObject)) return;
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
{
HighlightText(VisualTreeHelper.GetChild(dependencyObject, i), textToHighlight);
}
}
}
事实上,两个json模式都已生成,但测试运行程序无法找到此类文件。 这是gradle设置:
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
通过查看Google会议室迁移示例,我可以看到有区别,而且没有在谈论名称。
因此,很明显gradle插件所做的事情与我所认为的不同,或者文档说它应该可以工作,但是没有。
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"
}
答案 0 :(得分:4)
使用PaulWoitaschek发现的解决方法对我来说适用于Robolectric 4.3:
在应用程序的build.gradle
上的debug souceSets上添加纲要目录
时髦
sourceSets {
debug.assets.srcDirs += files("$projectDir/schemas".toString())
}
科特林DSL
sourceSets {
getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
}
答案 1 :(得分:1)
在创建 MigrationTestHelper
时也请确保插入实际的 database.class
。我感到困惑并改为设置我的 Entity.class
,导致同样的错误。
答案 2 :(得分:0)
您应该将其包装在 android 中:
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation":
"$projectDir/schemas".toString()]
}
}
}
}
然后构建项目
答案 3 :(得分:0)
尝试使用此:
sourceSets {
androidTest.assets.srcDirs += files("store".toString())
}
根据the documentation,自变量为
一个CharSequence,包括String或GString。根据文件(对象)解释相对到项目目录
(重点是我的)