I'm trying to test Retrofit api with Spek
It throws nullPointerException on the on{...} block
Associated stacktrace : https://pastebin.com/gy6dLtGg
Here’s my test class
@RunWith(JUnitPlatform::class)
class AccountCheckViewModelTest : Spek({
include(RxSchedulersOverrideRule)
val httpException = mock<HttpException> {
on { code() }.thenReturn(400)
}
given(" account check view model") {
var accountCheckRequest = mock<CheckExistingAccountRequest>()
var accountCheckResponse = mock<CheckExistingAccountResponse>()
var webService = mock<IAPICalls>()
val accountCheckViewModel = spy(VMAccountCheck(webService))
beforeEachTest {
accountCheckRequest = mock<CheckExistingAccountRequest>() {
on { email }.thenReturn("foo@mail")
}
accountCheckResponse = mock<CheckExistingAccountResponse>() {
on { firstName }.thenReturn("foo")
on { email }.thenReturn("foo@mail")
}
webService = mock<IAPICalls> {
on { checkExistingAccount(accountCheckRequest) }.thenReturn(Flowable.just(accountCheckResponse))
}
}
on("api success") {
accountCheckViewModel.checkIfAccountExists(request = accountCheckRequest)
it("should call live data with first name as foo") {
verify(accountCheckViewModel, times(1)).updateLiveData(accountCheckResponse.firstName, accountCheckResponse.email, null)
}
}
}
}
Here is my RxSchedulersOverrideSpek class
class RxSchedulersOverrideSpek : Spek({
beforeGroup {
RxJavaPlugins.onIoScheduler(Schedulers.trampoline())
RxJavaPlugins.onComputationScheduler(Schedulers.trampoline())
RxJavaPlugins.onNewThreadScheduler(Schedulers.trampoline())
}
})