我正在使用Kotlin,RxJava2,Retrofit和MVP模式开发一个Android应用。
我正在为Presenter编写JUnit测试代码。
@Mock
lateinit var mockRestService: RestService
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
mResPresenter = MainPresenter(
view = mockView,
restService = mockRestService
)
}
@Test
fun test1() {
val id = "id_01"
val res = Res()
// I am mocking in here:
Mockito.`when`(mockRestService.updateRes(id, res)).thenReturn(Observable.just(res))
Mockito.`when`(mockRestService.operateRes(id)).thenReturn(Observable.just(res))
mResPresenter.operateRes(res)
Mockito.verify(mockView).enableCancelButton(View.VISIBLE)
Mockito.verify(mockView).onResOperating(res)
}
但是我的Presenter代码中发生了NullPointerException。
java.lang.NullPointerException:source2为空
下面是我的演示者代码。
class MainPresenter(
val view: MainContract.View,
val restService: RestService
): MainContract.Presenter {
override fun operateRes(vararg res: Res) {
view.enableCancelButton(View.VISIBLE)
disposable.add(
Observable.fromIterable(res.toList())
.concatMap { res ->
val updateRes = restService.updateRes(id, res)
val operateRes = restService.operateRes(id) // I am mocking it, but this is null... why?...
Observable.concat(updateRes, operateRes) // NPE occurred in here
}
.window(2)
.subscribeOn(Schedulers.io())
.subscribe({
disposable.add(
it.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
view.onResOperating(it)
}
)
})
)
}
}
根据异常消息,“ operateRes”为空。 但是我在嘲笑它。
出了什么问题?... 我该如何解决?...