Apr444
Apr444

Reputation: 53

Mockito thenReturn not working as expected

I'm having problem with mockito. I'm mocking a class and then using thenReturn() on on of its method. but seems like something is going wrong. here is the code.

TestCode:

 public void getCardsTest() {
    FeatureFragmentPresenterImpl presenter = new FeatureFragmentPresenterImpl();
    GroupFeatureData data = Mockito.mock(GroupFeatureData.class);

    FeatureFragmentView view = Mockito.mock(FeatureFragmentView.class);

    presenter.init(view, data);
    Observable<Response<ResponseBody>> errorObservable = Observable.error(new IOException());
    assertNotNull(observable);
    Mockito.when(data.getCards(Mockito.anyString(), Mockito.anyString(),
            Mockito.anyInt(), Mockito.anyInt())).
            thenReturn(errorObservable);

    presenter.getAllCards(new Contact(new Name("ssd")), -1);     
}

Presenter code :

public void getAllCards(IContact iContact, int lastIndex) {

    Observable<Response<ResponseBody>> allCardsResponseObservable = mGroupFeatureData.getCards(path, id, 10, lastIndex);
    allCardsResponseObservable
            .subscribeOn(Schedulers.io()) -------> Test Failing because NPE here
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new DisposableObserver<Response<ResponseBody>>() {
                @Override
                public void onNext(@NonNull Response<ResponseBody> response) {

                }

                @Override
                public void onError(@NonNull Throwable e) {

                }

                @Override
                public void onComplete() {

            });
}

  public void init(FeatureFragmentView featureFragmentView,
                  GroupFeatureData groupFeatureData) {
    this.mGroupFeatureData = groupFeatureData;
    this.mFeatureFragmentView = featureFragmentView;
}

Even though i'm mocking response of data.getCards() in Test, In presenter it is throwing NPE whereas it should just operate on mocked Observable that is errorObservable. what is going wrong here?

Upvotes: 0

Views: 1973

Answers (1)

glytching
glytching

Reputation: 47905

The NPE tells us that this line:

mGroupFeatureData.getCards(path, id, 10, lastIndex);

... returns null which implies that the actual method call and the method call which you mocked here ...

Mockito.when(data.getCards(Mockito.anyString(), Mockito.anyString(),
        Mockito.anyInt(), Mockito.anyInt())).
        thenReturn(errorObservable);

... do not match. The code supplied shows this actual call:

Observable<Response<ResponseBody>> allCardsResponseObservable = 
    mGroupFeatureData.getCards(path, id, 10, lastIndex);

Breaking this call down we can say that:

  • The third argument 10 is an int so this will match the given argument matcher: Mockito.anyInt()
  • The fourth argument lastIndex is declared as an int so this will match the given argument matcher: Mockito.anyInt()
  • The type of the first and second argments is not clear from your code extract since we do not see where path and id are declared but unless they are both of type String then the given argument matchers for these parameters (Mockito.anyString()) will not match and hence the mocked call will return null.

So, it looks to me like one or other of path and id are not actually of type String. It would be useful if you could update your question to show where these types are declared.

Upvotes: 1

Related Questions