Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android `suspendCoroutine`throws`此作业尚未完成`exception?_Android_Kotlin_Kotlin Coroutines_Android Testing_Google Mlkit - Fatal编程技术网

Android `suspendCoroutine`throws`此作业尚未完成`exception?

Android `suspendCoroutine`throws`此作业尚未完成`exception?,android,kotlin,kotlin-coroutines,android-testing,google-mlkit,Android,Kotlin,Kotlin Coroutines,Android Testing,Google Mlkit,我正在尝试测试我的封装的translator类,它在引擎盖下使用Google ML Kit Translate。我收到一个抛出java.lang.IllegalStateException的错误:此作业尚未完成。我不太明白为什么会发生这种异常。有人有什么想法吗 导入com.google.common.truth.truth.assertThat 导入kotlinx.coroutines.CoroutineDispatcher 导入kotlinx.coroutines.Dispatchers 导入

我正在尝试测试我的封装的translator类,它在引擎盖下使用Google ML Kit Translate。我收到一个抛出
java.lang.IllegalStateException的错误:此作业尚未完成
。我不太明白为什么会发生这种异常。有人有什么想法吗

导入com.google.common.truth.truth.assertThat
导入kotlinx.coroutines.CoroutineDispatcher
导入kotlinx.coroutines.Dispatchers
导入kotlinx.coroutines.experimentalRoutinesAPI
导入kotlinx.coroutines.runBlocking
导入kotlinx.coroutines.test.TestCoroutineDispatcher
导入kotlinx.coroutines.test.resetMain
导入kotlinx.coroutines.test.runBlockingTest
导入kotlinx.coroutines.test.setMain
导入org.junit.Before
导入org.junit.Rule
导入org.junit.Test
导入org.junit.rules.TestWatcher
导入org.junit.runner.Description
进口木材
导入kotlin.coroutines.resume
导入kotlin.coroutines.suspendCoroutine
GoogleMLKitTranslatorTest类{
私有lateinit变量转换器:GoogleMLKitTranslator
private val testValue=“Bana herhangi birşey sor”
@获取:规则
var coroutinestrule=coroutinestrule()
@以前
趣味设置(){
translator=GoogleMLKitTranslator()
translator.initialize()
木材.植物(木材.树木())
}
@试验
有趣的翻译土耳其语测试到英语应该返回真=
coroutinestrule.testDispatcher.runBlockingTest{
val expectedValue=“问我任何问题”
val translatedText=suspendCoroutine{continuation->
翻译(
value=testValue,
onSuccess={
继续。继续(it)
},
onFailure={
继续。继续(it.message?:“哇,有点不对劲!”
}
)
}
Timber.d(“翻译文本为$translatedText”)
assertThat(translatedText).matches(expectedValue)
}
@实验常规
类coroutinestrule(val testDispatcher:TestCoroutineDispatcher=TestCoroutineDispatcher()):TestWatcher(){
覆盖乐趣启动(说明:说明?){
超级启动(说明)
Dispatchers.setMain(testDispatcher)
}
覆盖乐趣已完成(说明:说明?){
超级。完成(说明)
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()文件
}
}
}

使用
runBlocking()
而不是
coroutinestrule.testDispatcher.runBlockingTest()
以某种方式解决了这个问题,我无法提供任何进一步的见解,但关于coroutines repo还有一个未解决的问题,需要提供更多信息
java.lang.IllegalStateException: This job has not completed yet
    at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1128)
    at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53)
    at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:80)
    at com.theteampotato.gifit.translate.GoogleMLKitTranslatorTest.translate_turkish_test_to_english_should_return_true(GoogleMLKitTranslatorTest.kt:46)
    at java.lang.reflect.Method.invoke(Native Method)
    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 androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
    at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:388)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2258)