Skip to content Skip to sidebar Skip to footer

Rxjava Android Mvp Unit Test Nullpointerexception

I am new to Unit Testing in mvp, I want to make a verrry basic test to the presenter, which is responsible for making login, I just want to assert on view.onLoginSuccess(); Here i

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"