首页 > 解决方案 > 在单元测试中订阅 Observable 时出现 NullPointerException

问题描述

我想在我的单元测试中订阅一个 observable。这是我的代码:

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不为空。

标签: androidunit-testingkotlinmockito

解决方案


我的答案迟到了,但它可能会对某人有所帮助。

结果viewModel.predictionNominatimsObservable是 null 所以当你调用 subscribe 它只会抛出 NPE。

相反,您需要存根此方法的结果。为了能够使用存根,您需要将 ViewModel 设为模拟。你需要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 做什么,然后调用该方法。

viewModel.predictionNominatimsObservable.subscribe(predictionsObserver)

推荐阅读