Espresso 中错误线程的领域访问
Realm access from incorrect thread in Espresso
在每次浓缩咖啡测试之前,我有一个注释 @Before
,我在其中初始化我的 RealmManager.realm
。
我的代码片段 object Realm
:
init {
Realm.init(SaiApplication.context)
val builder = RealmConfiguration.Builder().schemaVersion(SCHEMA_VERSION)
builder.migration(runMigrations())
if (!BuildConfig.DEBUG) builder.encryptionKey(getOrCreateDatabaseKey())
if (SaiApplication.inMemoryDatabase) builder.inMemory()
Realm.setDefaultConfiguration(builder.build())
try {
errorOccurred = false
realm = Realm.getDefaultInstance()
} catch (e: Exception) {
errorOccurred = true
realm = Realm.getInstance(RealmConfiguration.Builder()
.schemaVersion(SCHEMA_VERSION).name(errorDbName).build())
e.log()
deleteRealmFile(realm.configuration.realmDirectory)
}
}
但是当我 运行 我的测试时,我得到下一个错误:
Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created
那么我如何才能在我的测试中正确地初始化我的领域?
我发现其中一个 solutions 很有趣,创建一个假的 init 领域。
要从 UI 测试中操作 UI 线程的 Realm 实例,您需要使用 instrumentation.runOnMainSync(() -> {...});
.[=12 在 UI 线程上初始化 Realm 实例=]
@Before
public void setup() {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
// setup UI thread Realm instance configuration
}
});
}
我做什么。
我刚刚在我的 AppTools 中添加了下一个功能,它通过测试检查包:
fun isTestsSuite() = AppResources.appContext?.classLoader.toString().contains("tests")
然后修改了Realm的初始化:
init {
Realm.init(AppResources.appContext)
val builder = RealmConfiguration.Builder().schemaVersion(SCHEMA_VERSION)
builder.migration(runMigrations())
if (!isTestsSuite()) builder.encryptionKey(getOrCreateDatabaseKey()) else builder.inMemory()
Realm.setDefaultConfiguration(builder.build())
try {
errorOccurred = false
realm = Realm.getDefaultInstance()
} catch (e: Exception) {
errorOccurred = true
realm = Realm.getInstance(RealmConfiguration.Builder()
.schemaVersion(SCHEMA_VERSION).name(errorDbName).build())
e.log()
deleteRealmFile(realm.configuration.realmDirectory)
}
}
在每次浓缩咖啡测试之前,我有一个注释 @Before
,我在其中初始化我的 RealmManager.realm
。
我的代码片段 object Realm
:
init {
Realm.init(SaiApplication.context)
val builder = RealmConfiguration.Builder().schemaVersion(SCHEMA_VERSION)
builder.migration(runMigrations())
if (!BuildConfig.DEBUG) builder.encryptionKey(getOrCreateDatabaseKey())
if (SaiApplication.inMemoryDatabase) builder.inMemory()
Realm.setDefaultConfiguration(builder.build())
try {
errorOccurred = false
realm = Realm.getDefaultInstance()
} catch (e: Exception) {
errorOccurred = true
realm = Realm.getInstance(RealmConfiguration.Builder()
.schemaVersion(SCHEMA_VERSION).name(errorDbName).build())
e.log()
deleteRealmFile(realm.configuration.realmDirectory)
}
}
但是当我 运行 我的测试时,我得到下一个错误:
Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created
那么我如何才能在我的测试中正确地初始化我的领域?
我发现其中一个 solutions 很有趣,创建一个假的 init 领域。
要从 UI 测试中操作 UI 线程的 Realm 实例,您需要使用 instrumentation.runOnMainSync(() -> {...});
.[=12 在 UI 线程上初始化 Realm 实例=]
@Before
public void setup() {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
// setup UI thread Realm instance configuration
}
});
}
我做什么。 我刚刚在我的 AppTools 中添加了下一个功能,它通过测试检查包:
fun isTestsSuite() = AppResources.appContext?.classLoader.toString().contains("tests")
然后修改了Realm的初始化:
init {
Realm.init(AppResources.appContext)
val builder = RealmConfiguration.Builder().schemaVersion(SCHEMA_VERSION)
builder.migration(runMigrations())
if (!isTestsSuite()) builder.encryptionKey(getOrCreateDatabaseKey()) else builder.inMemory()
Realm.setDefaultConfiguration(builder.build())
try {
errorOccurred = false
realm = Realm.getDefaultInstance()
} catch (e: Exception) {
errorOccurred = true
realm = Realm.getInstance(RealmConfiguration.Builder()
.schemaVersion(SCHEMA_VERSION).name(errorDbName).build())
e.log()
deleteRealmFile(realm.configuration.realmDirectory)
}
}