0

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

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
Rahul
  • 3,293
  • 2
  • 31
  • 43

1 Answers1

1

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();

}
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • same issue again "Wanted but not invoked:mSignInView.showLoading();" – Rahul Jan 09 '18 at 12:32
  • have you checked that your ValidationUtil checks pass and method does not return? – Maciej Kowalski Jan 09 '18 at 12:33
  • i have already created different test cases for ValidationUtils Class.Do i need to paste that code here – Rahul Jan 09 '18 at 12:43
  • not really. just make sure the `ValidationUtil.checkEmail(email)` and `ValidationUtil.checkPassword(password)` evaluate to true, otherwise the test will never succeed. Possibly you need to pass something else than empty strings as input :`""` – Maciej Kowalski Jan 09 '18 at 12:50
  • Okey let me try that without empty strings – Rahul Jan 09 '18 at 13:02
  • it works....but i want to test if user call this method with empty string "" then run showErrorMessage method else run show loading – Rahul Jan 17 '18 at 04:56