Rxjava Android Mvp Unit Test Nullpointerexception
Solution 1:
The problem is your TestScheduler
. You should create a helper class to provide schedulers
for your observable
. Something likes:
classRxProvider{
funprovideIOScheduler()funprovideAndroidMainScheduler()
}
//Then you can call the rxprovider inside your presenter:
loginModel.loginUser(loginRequest)
.subscribeOn(rxProvider.provideIOScheduler())
.observeOn(rxProvider.provideAndroidMainScheduler())
// inside your test caseswhen(rxProvider....).thenReturn(testSchedulers)
P/s: 1 more tip, you should mock your LoginResponse
instead of calling new
everywhere.
Solution 2:
You made a TestScheduler in your test class, but your Presenter doesn't know about it. Just like you have your view, model, localModel, and compositeDisposable as dependencies of the presenter, you need to add two new dependencies: Your IO Scheduler (Schedulers.computation()
in the non test code, and new TestScheduler()
in the test code) and your UI Scheduler (AndroidSchedulers.mainThread()
in the non test code, and new TestScheduler()
in the test code).
Once you do that, you can set up 2 new TestSchedulers in your test code. Declare them as testIoScheduler = new TestScheduler()
and testUiScheduler = new TestScheduler()
. Then, right after you call the method under test (SUT.loginPres(any());
), you can invoke the schedulers with testIoScheduler.triggerActions()
and testUiScheduler.triggerActions()
Post a Comment for "Rxjava Android Mvp Unit Test Nullpointerexception"