在Kotlin中通过JUnit测试LiveData时,我面临一个奇怪的问题,即日志显示的期望值和实际值完全相似,但Test Observer仍未通过测试。
下面是测试,并在运行测试后记录日志。
@Test
fun onSelectedAddressInfoTest_selected() {
viewModel.selectedAddressInfo?.test()?.assertNoValue()
viewModel.onAddressInfoSelected(AddressInfoStub.getValidStub())
viewModel.selectedAddressInfo?.test()?.assertHasValue()
//error is here
// viewModel.selectedAddressInfo?.test()?.assertValue(AddressInfoStub.getValidStub())
}
输出:
java.lang.AssertionError:
Expected: AddressInfo(id=1, imageUrl=image.com, description=Deliver to hukum, location=Location{address = 'place',lng = '12.999999',lat = '24.0'}) (class: AddressInfo)
Actual: AddressInfo(id=1, imageUrl=image.com, description=Deliver to hukum, location=Location{address = 'place',lng = '12.999999',lat = '24.0'}) (class: AddressInfo)
at com.jraska.livedata.TestObserver.fail(TestObserver.java:268)
at com.jraska.livedata.TestObserver.assertValue(TestObserver.java:105)
at com.assessment.AddressInfobaseproject.ui.AddressInfoList.AddressInfoListViewModelTest.onSelectedAddressInfoTest_selected(AddressInfoListViewModelTest.kt:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)