Hot questions for Handling NullPointerException in RxJava

Top 10 Java Open Source / RxJava / Handling NullPointerException

RxJava android mvp unit test NullPointerException

Question: 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 is the PresenterCode:

public LoginPresenter(LoginViewContract loginView,
                      LoginModelContract loginModel,
                      CurrentUserLoginModelContract currentUserLoginModel,
                      CompositeDisposable subscriptions) {

    this.subscriptions = subscriptions;

    this.loginView = loginView;

    this.loginModel = loginModel;

    this.currentUserLoginModel = currentUserLoginModel;
}

@Override
public void loginPres(LoginRequest loginRequest) {
    loginModel.loginUser(loginRequest)
        .subscribeOn(Schedulers.computation())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new SingleObserver<LoginResponse>() {

            @Override
            public void onSubscribe(Disposable d) {
                subscriptions.add(d);
            }

            @Override
            public void onSuccess(LoginResponse loginResponse) {
            //  do something with the response
                loginView.loginSuccessMessage(token, true);
            }

            @Override
            public void onError(Throwable e) {
                loginView.loginFailedErrorMessage();
                Timber.e(e, "Error while trying to login");
            }
        });
}

Android here is the test code:

@RunWith(MockitoJUnitRunner.class)
public class LoginPresenterTest {

@Mock
LoginViewContract view;
@Mock
LoginModelContract model;
@Mock
CurrentUserLoginModelContract localModel;

LoginPresenter SUT;

@Before
public void setUp() throws Exception {
    compositeDisposable = new CompositeDisposable();
    SUT = new LoginPresenter(view, model, localModel, compositeDisposable);
}

@Test
public void name() {
    LoginResponse singleResponse = new LoginResponse();

    TestScheduler testScheduler = new TestScheduler();

    when(model.loginUser(any()))
            .thenReturn(Single.just(new LoginResponse()));

    SUT.loginPres(any());
}

It just gives me NullPointerException, I think that Knowing how to test the success will help me test everything else, I have read about the TestScheduler, I tried it but did not help, I think I am doing something wrong, thanks in advance.

Answer: The problem is your TestScheduler. You should create a helper class to provide schedulers for your observable. Something likes:

class RxProvider{
     fun provideIOScheduler()
     fun provideAndroidMainScheduler()
}

//Then you can call the rxprovider inside your presenter:
loginModel.loginUser(loginRequest)
    .subscribeOn(rxProvider.provideIOScheduler())
    .observeOn(rxProvider.provideAndroidMainScheduler())
// inside your test cases
when(rxProvider....).thenReturn(testSchedulers)

you should mock your LoginResponse instead of calling new everywhere.