下面的测试会导致随机崩溃。我大部分时间都不知所措,它工作正常。
> @Test fun `perform login without a previously saved user`() =
> runBlocking {
> testSavedUser1LiveData = MutableLiveData()
>
> coEvery { userDao.getCurrentUserLiveData() } returns testSavedUser1LiveData
> coEvery { userDao.upsert(any()) } returns Unit
>
> coEvery { userAPIDataSource.fetchLoginUser(loginCredentials) } returns Unit
> coEvery { userAPIDataSource.getLoginUserResults } returns testUser1ResponseLiveDataOfJsonElement
>
> loginRepository = LoginRepositoryImpl(userDao, companyDao, userAPIDataSource)
> loginViewModel = LoginViewModel(dispatchers, validator, loginRepository)
>
> loginViewModel.loginLiveData().observeForever(loginLiveDataObserver)
> loginViewModel.login(loginCredentials)
>
> coVerifyOrder {
> loginLiveDataObserver.onChanged(enableLoading)
> loginLiveDataObserver.onChanged(loginResults)
> loginLiveDataObserver.onChanged(disableLoading)
> } }
附带的代码很有趣 login(credentials: LoginCredentials) { _loginLiveData.value = LoginResult.Loading(true)
if(validator.validate(credentials)) {
triggerLogin(credentials)
} else {
_loginLiveData.value = LoginResult.Failure("Incorrect Credentials")
_loginLiveData.value = LoginResult.Loading(false)
}
}
private fun triggerLogin(credentials: LoginCredentials) {
launch(dispatchers.background) {
loginRepository.performLogin(credentials)
withContext(dispatchers.ui) {
_loginLiveData.value = loginRepository.getLoginUserResults
_loginLiveData.value = LoginResult.Loading(false)
}
}
}
崩溃是:
java.lang.AssertionError:验证失败:发生的调用少于订单验证序列要求的调用。
匹配器:Observer(#94).onChanged(eq(Loading(value=true)))) Observer(#94).onChanged(eq(Success(user=Current User Response: 1: testUserName1)))) Observer(#94 ).onChanged(eq(Loading(value=false))))
调用:1) Observer(#94).onChanged(Loading(value=true)) 2) Observer(#94).onChanged(Success(user=Current User Response: 1: testUserName1))
堆栈跟踪: 1)
io.mockk.impl.InternalPlatform.captureStackTrace
(InternalPlatform.kt:114)
io.mockk.impl.stub.MockKStub.handleInvocation
(MockKStub.kt:256)
io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler $1.invocation (JvmMockFactoryHelper.kt:25)
io.mockk.proxy.jvm.advice.Interceptor.call
(Interceptor.kt:20)
io.mockk.proxy.jvm.advice.BaseAdvice.handle
(BaseAdvice.kt:42)
io.mockk.proxy.jvm.advice.jvm.JvmMockKProxyInterceptor.interceptNoSuper (JvmMockKProxyInterceptor.java:45)
androidx.lifecycle.Observer$Subclass18.onChanged
(-:-1)
androidx.lifecycle.LiveData.considerNotify
(LiveData.java:131 )
androidx.lifecycle.LiveData.dispatchingValue
(LiveData.java:149)
androidx.lifecycle.LiveData.setValue
(LiveData.java:307)
androidx.lifecycle.MutableLiveData.setValue
(MutableLiveData.java:50)
com.test.testapp.ui.login.LoginViewModel.login
(LoginViewModel.kt:84)
com.test.testapp.featureTests.login.LoginFeatureShould$perform login without a之前保存的用户$1.invokeSuspend
(LoginFeatureShould.kt:102)
kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith
(ContinuationImpl.kt:32)
kotlinx.coroutines.DispatchedTask.run
(Dispatched.kt:233)
kotlinx.coroutines.EventLoopImplBase.processNextEvent
(EventLoop.kt:116)
kotlinx.coroutines.BlockingCoroutine.joinBlocking
(Builders.kt:76)
kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking
(Builders.kt:53)
kotlinx.coroutines.BuildersKt.runBlocking
( -:1)
kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default
(Builders.kt:35)
kotlinx.coroutines.BuildersKt.runBlocking$default
(-:1)
com.test.testapp.featureTests.login.LoginFeatureShould.perform login 没有以前保存的用户 (LoginFeatureShould.kt:89)sun.reflect.NativeMethodAccessorImpl.invoke0
(NativeMethodAccessorImpl.java:-2)N
sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke
(Method.java :498)
org.junit.platform.commons.util.ReflectionUtils.invokeMethod
(ReflectionUtils.java:628)
org.junit.jupiter.engine.execution.ExecutableInvoker.invoke
(ExecutableInvoker.java:117)
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7 (TestMethodTestDescriptor.java:184)
org.junit.platform.engine .support.hierarchical.ThrowableCollector.execute (ThrowableCollector.java:73)
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod (TestMethodTestDescriptor.java:180)
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute
(TestMethodTestDescriptor.java:127)
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute
(TestMethodTestDescriptor.java:68)
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5 (NodeTestTask.java: 135)
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute (ThrowableCollector.java:73)
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7 (NodeTestTask.java:125)
org.junit.platform.engine.support.hierarchical.Node.around
(Node.java:135)
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8 (NodeTestTask.java:123)
org.junit .platform.engine.support.hierarchical.ThrowableCollector.execute (ThrowableCollector.java:73)
org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively (NodeTestTask.java:122)
org.junit.platform.engine.support .hierarchical.NodeTestTask.execute
(NodeTestTask.java:80)
java.util.ArrayList.forEach
(ArrayList.java:1251)
org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll (SameThreadHierarchicalTestExecutorService.java:38) org.junit.platform.engine.support.hierarchical.NodeTestTask。 lambda$executeRecursively$5 (NodeTestTask.java:139)
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute (ThrowableCollector.java:73)
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7 (NodeTestTask.java:125)
org.junit.platform.engine.support.hierarchical.Node.around
(Node.java:135)
org.junit .platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8 (NodeTestTask.java:123)
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute (ThrowableCollector.java:73)
org.junit.platform。 engine.support.hierarchical.NodeTestTask.executeRecursively (NodeTestTask.java:122)
org.junit.platform.engine.support.hierarchical.NodeTestTask.execute
(NodeTestTask.java:80)
java.util.ArrayList.forEach
(ArrayList.java:1251)
org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService。 invokeAll (SameThreadHierarchicalTestExecutorService.java:38) org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5 (NodeTestTask.java:139)
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute (ThrowableCollector .java:73)
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7 (NodeTestTask.java:125)
org.junit.platform.engine.support.hierarchical.Node.around
(Node.java:135)
org.junit .platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8 (NodeTestTask.java:123)
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute (ThrowableCollector.java:73)
org.junit.platform。 engine.support.hierarchical.NodeTestTask.executeRecursively (NodeTestTask.java:122)
org.junit.platform.engine.support.hierarchical.NodeTestTask.execute
(NodeTestTask.java:80)
org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit (SameThreadHierarchicalTestExecutorService.java:32) org.junit.platform。 engine.support.hierarchical.HierarchicalTestExecutor.execute (HierarchicalTestExecutor.java:57)
org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute (HierarchicalTestEngine.java:51)
org.junit.platform.launcher.core.DefaultLauncher。执行
(DefaultLauncher.java:229)
org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6
(DefaultLauncher.java:197)
org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams (DefaultLauncher.java:211)
org.junit.platform.launcher .core.DefaultLauncher.execute
(DefaultLauncher.java:191)
org.junit.platform.launcher.core.DefaultLauncher.execute
(DefaultLauncher.java:128)
com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs
(JUnit5IdeaTestRunner.java:74)
com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs (IdeaTestRunner.java:47)
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart
(JUnitStarter.java:242)
com .intellij.rt.execution.junit.JUnitStarter.main
(JUnitStarter.java:70)2)
io.mockk.impl.InternalPlatform.captureStackTrace
(InternalPlatform.kt:114)
io.mockk.impl.stub.MockKStub.handleInvocation
(MockKStub.kt:256)
io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation (JvmMockFactoryHelper.kt:25)
io.mockk.proxy.jvm.advice.Interceptor.call (Interceptor.kt:20)
io.mockk.proxy.jvm.advice.BaseAdvice.handle
(BaseAdvice.kt:42)
io.mockk .proxy.jvm.advice.jvm.JvmMockKProxyInterceptor.interceptNoSuper (JvmMockKProxyInterceptor.java:45) androidx.lifecycle.Observer$Subclass18.onChanged
(-:-1)
androidx.lifecycle.LiveData.considerNotify
(LiveData.java:131)
androidx.lifecycle.LiveData.dispatchingValue
(LiveData.java:149)
androidx.lifecycle.LiveData.setValue
(LiveData.java:307)
androidx.lifecycle.MutableLiveData.setValue
( MutableLiveData.java:50)
com.test.testapp.ui.login.LoginViewModel$triggerLogin$1$1.invokeSuspend (LoginViewModel.kt:99)
com.test.testapp.ui.login.LoginViewModel$triggerLogin$1$1.invoke
(LoginViewModel.吨:-1)
kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn (Undispatched.kt:91)
kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext
(Builders.common.kt:146)
kotlinx.coroutines.BuildersKt.withContext (-:1)com.test.testapp.ui.login.LoginViewModel$triggerLogin$1.invokeSuspend
(LoginViewModel.kt:98)
kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith
(ContinuationImpl.kt:32)
kotlinx.coroutines.DispatchedTask.run (Dispatched.kt:233)
kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely
(CoroutineScheduler .kt:594)
kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely
(CoroutineScheduler.kt:60)
kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run (CoroutineScheduler.kt:742)在 io.mockk.impl.recording.states.VerifyingState.failIfNotPassed(VerifyingState.kt:66) 在 io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:42) 在 io.mockk.impl.recording .CommonCallRecorder.done(CommonCallRecorder.kt:48) at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60) at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:27) at io.mockk.MockKDsl.internalCoVerify(API.kt:143) at io.mockk.MockKDsl.internalCoVerify$default(API.kt:134) at io.mockk.MockKDsl.internalCoVerifyOrder(API.kt:212) at io.mockk .MockKKt.coVerifyOrder(MockK.kt:264) at io.mockk.MockKKt.coVerifyOrder$default(MockK.kt:261) at com.test.testapp.featureTests.login.LoginFeatureShould$perform login without a previously saved user$1。invokeSuspend(LoginFeatureShould.kt:104) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32) at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233) at kotlinx.coroutines.EventLoopImplBase.processNextEvent (EventLoop.kt:116) at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:76) at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:53) at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source) at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:35) at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source) at com.test.testapp.featureTests.login.LoginFeatureShould.perform login without a以前保存的用户(LoginFeatureShould.kt:89) 在 sun.reflect.NativeMethodAccessorImpl。在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java: 43) 的 invoke0(Native Method) 498)在 org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:117)在 org.junit.jupiter .engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:184) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.jupiter.engine。 org.junit.jupiter.engine 中的描述符.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:180)。descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:127) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$ executeRecursively$5(NodeTestTask.java:135) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively $7(NodeTestTask.java:125) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8 (NodeTestTask.java:123) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) 在 java .util.ArrayList.forEach(ArrayList.java:1251) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) at org.junit.platform.engine.support.hierarchical.NodeTestTask .lambda$executeRecursively$5(NodeTestTask.java:139) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask。 lambda$executeRecursively$7(NodeTestTask.java:125) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73 ) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) 在 java 的 org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)。 org.junit.platform.engine.support.hierarchical.NodeTestTask 的 org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) 的 util.ArrayList.forEach(ArrayList.java:1251)。 org.junit.platform.engine.support.hierarchical.ThrowableCollector 上的 lambda$executeRecursively$5(NodeTestTask.java:139)。在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) 在 org.junit.platform.engine.support.hierarchical.Node.around 执行(ThrowableCollector.java:73) (Node.java:135) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute( ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java: 80) 在 org.junit 的 org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)。platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) 在 org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) 在 org.junit.platform.launcher。 core.DefaultLauncher.execute(DefaultLauncher.java:229) at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197) at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams (DefaultLauncher.java:211) 在 org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191) 在 org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) 在com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74) 在 com.intellij.rt.execution.junit。IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.爪哇:70)