在单元测试中订阅 Observable 时的 NullPointerException



我想在我的单元测试中订阅一个可观察量。这是我的代码:

private lateinit var viewModel: AddressSearchViewModel
private lateinit var ioScheduler: TestScheduler
private lateinit var scheduler: TestScheduler
private lateinit var requestHelper: RequestHelper
@Test
fun testAutoComplete2Chars() {
MockitoAnnotations.initMocks(this)
requestHelper = RequestHelper(createConfiguration())
ioScheduler = Schedulers.test()
scheduler = Schedulers.test()
val country = getCountry("NL")
createViewModel(country)
viewModel.predict("Br")
////// Error on this line!!
viewModel.predictionNominatimsObservable.subscribe(predictionsObserver)
ioScheduler.triggerActions()
scheduler.triggerActions()
verify(predictionsObserver, times(1)).onNext(viewModel.predictionNominatimsObservable.value)
assertEquals(0, viewModel.predictionNominatimsObservable.value?.size ?: 0)
}
private fun createViewModel(country: Country) {
viewModel = AddressSearchViewModel(country)
viewModel.ioScheduler = ioScheduler
viewModel.scheduler = scheduler
}

堆栈跟踪:

java.lang.NullPointerException
at rx.Subscriber.isUnsubscribed(Subscriber.java:108)
at rx.Observable.subscribe(Observable.java:10358)
at rx.Observable.subscribe(Observable.java:10319)
at com.takeaway.android.core.view_model.AddressSearchViewModelTest.testAutoComplete2Chars(AddressSearchViewModelTest.kt:529)
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.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)
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 com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

我无法弄清楚我的代码出了什么问题。有人可以帮忙吗?我可以确认viewModel.predictionNominatimsObservable不是空的。

我回答得很晚,但它可能会帮助某人。

viewModel.predictionNominatimsObservable的结果为 null,因此当您调用订阅时,它只会抛出 NPE。

相反,您需要存根此方法的结果。为了能够使用存根,您需要将视图模型设置为模拟。你需要spy它。所以这条线会像这样改变

viewModel = AddressSearchViewModel(country) // before
viewModel = spy(AddressSearchViewModel(country)) //after

之后,您可以像这样存根所需的行为

`when`(viewModel.predictionNominatimsObservable).thenReturn(Observable.just(fakeExpectedResponse))

或者您也可以测试错误或空案例,例如

`when`(viewModel.predictionNominatimsObservable).thenReturn(Observable.empty()) 
`when`(viewModel.predictionNominatimsObservable).thenReturn(Observable.error(expectedException))

此存根方法应放在"真实"调用之前。因此,首先你告诉你的模拟视图模型要做什么,然后调用该方法。

viewModel.predictionNominatimsObservable.subscribe(predictionsObserver)

最新更新