Rahul
Rahul

Reputation: 3349

how to test presenter class using mockito in model-view-presenter architecture android

I want to create a test for presenter class using mockito tried few ways but getting error mention below. I am following these links here and here and my presenter class

public class SignInPresenterImpl extends BasePresenterImpl implements SignInPresenter {

    private SignInView mSignInView;
    private SignInInteractor mSignInInteractor;

    /**
     * Constructor
     *
     * @param signInView the associated SignIn view
     */
    public SignInPresenterImpl(@NonNull final SignInView signInView) {
        mSignInView = signInView;
        mSignInInteractor = new SignInInteractorImpl();

    }

    @Override
    public void onSignInClicked(final String email, final String password) {

        // checking for validation
        if (!ValidationUtil.checkEmail(email)) {
            mSignInView.showErrorMessage(R.string.error_invalid_email);
            return;
        }

        if (!ValidationUtil.checkPassword(password)) {
            mSignInView.showErrorMessage(R.string.error_invalid_password);
            return;
        }

        mSignInView.showLoading();
        mSignInInteractor.login(email, password, new BaseInteractor.ApiListener() {
            @Override
            public void onSuccess(final CommonResponse commonResponse) {
                //todo handle success
            }

            @Override
            public void onFailure(final ApiError apiError, final Throwable throwable) {

                if (isViewAttached()) {
                    mSignInView.hideLoading();
                    if (apiError != null) {
                        mSignInView.showErrorMessage(apiError.getMessage());
                    } else {
                        // resolve error through throwable
                        mSignInView.showErrorMessage(parseThrowableMessage(throwable));

                    }
                }
            }
        });
    }
}

and my presenter test class is

public class SignInPresenterImplTest {

    @Mock
    BaseInteractor.ApiListener listener;
    @Mock
    private SignInView mSignInView;
    @Mock
    private SignInInteractor mSignInInteractor;
    private SignInPresenter signInPresenter;

    @Before
    public void setUp() throws Exception {
        // Mockito has a very convenient way to inject mocks by using the @Mock annotation. To
        // inject the mocks in the test the initMocks method needs to be called.
        MockitoAnnotations.initMocks(this);

        //Get a refrence to the class test.
        signInPresenter = new SignInPresenterImpl(mSignInView);
    }

    @Test
    public void signInAndShowProgress() {
        signInPresenter.onSignInClicked("", "");

        mSignInView.showErrorMessage("");

        verify(mSignInView).showLoading("Loading");

    }
}

mSignInView shows below error

Wanted but not invoked: mSignInView.showLoading("Loading");

Please suggest me how to implement test cases in a correct way what I am doing wrong in it.

Thanks in advance

Upvotes: 0

Views: 893

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26522

In your method under test, the showLoading method is invoked with no attributes.. i think you should expect that and possibly verify that no error message has been shown:

@Test
public void signInAndShowProgress() {
    signInPresenter.onSignInClicked("", "");

    verify(mSignInView, times(0)).showErrorMessage(Mockito.any(String.class));

    verify(mSignInView).showLoading();

}

Upvotes: 1

Related Questions