在单元测试中订阅Observable时发生NullPointerException

时间:2018-08-21 14:51:08

标签: android unit-testing kotlin mockito

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

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

1 个答案:

答案 0 :(得分:0)

我的答案来晚了,但可能会对某人有所帮助。

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

相反,您需要对此方法的结果进行存根。为了能够使用存根,您需要将ViewModel模拟。您需要spy。所以这行会像这样

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

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

`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)