I am developing an android app using Kotlin, RxJava, and Retrofit.
I am sending a request to delete a resource.
HTTP - DELETE
And the response is 204 No Content. My retrofit code is below:
@DELETE("res/{resId}")
fun deleteJob(@Path("resId") resId: String): Observable<Unit>
In this case I don't know how to define the return type. So I defined "Observable". Because there are no response body. Response code is 204.
And below is my presenter code:
override fun deleteRes(resId: String) {
restService.deleteRes(resId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
// here is not called
}, {
// Always here is called, because the response is 204.
if (it is NoContentException) { // I defined new exception.
view.removeRes(resId)
} else {
Log.e(TAG, "deleteRes - failed: ${it.message}")
}
})
}
I want to test this Presenter function.
Below is my test code:
@Test
fun deleteResTest() {
val deleteResId = "delete_res_id"
Mockito.`when`(mockRestService.deleteRes(deleteResId)).thenReturn(Observable.just(Unit))
mockRestService.deleteRes(deleteResId)
.toFlowable(BackpressureStrategy.BUFFER)
.subscribe(TestSubscriber.create<Unit>())
mJobsPresenter.deleteRes(deleteResId)
Mockito.verify(mockView).removeRes(deleteResId)
}
But when I run this test code, it's failed like this:
Wanted but not invoked:
view.removeRes("delete_res_id");
-> at com.RessPresenterTest.deleteResTest(ResPresenterTest.kt:95)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
view.removeRes("delete_res_id");
-> at com.RessPresenterTest.deleteResTest(ResPresenterTest.kt:95)
Actually, there were zero interactions with this mock.
at com.RessPresenterTest.deleteResTest(ResPresenterTest.kt:95)
Somebody help me, please?
I suggest you to use
Completable
instead ofObservable
for "204 no content" responses, because these responses have not any content and we just need theonComplete
andonError
methods. so you can create aCompletable
and callonComplete
method in test.