title: Unit Testing MVP using mockito with event listeners
body: <pre><code>Android Studio 2.1.2
  </code></pre>
  
  <p>I want to test that the callbacks onUsernameError, onPasswordError, and onSuccess, in the LoginModelImp are actually called. I am not sure how to test event listeners. However, the test fails as those functions are never called. I am mocking them with mockito and trying to verify them.</p>
  
  <p>This is my code so far.</p>
  
  <p>Presenter interface</p>
  
  <pre><code>public interface LoginPresenterContract&lt;LoginFragmentViewContract&gt; {
      void validateCredentials();
  
      void attachView(LoginFragmentViewContract view);
      void detachView();
  }
  </code></pre>
  
  <p>Presenter implementation</p>
  
  <pre><code>public class LoginPresenterImp implements LoginPresenterContract&lt;LoginFragmentViewContract&gt;, LoginModelContract.OnLoginCompletedListener {
  
      private LoginModelContract mLoginModelContract;
      private LoginFragmentViewContract mLoginFragmentView;
  
      public LoginPresenterImp(LoginModelContract loginModelContract) {
          mLoginModelContract = loginModelContract;
      }
  
      /*
       * LoginPresenterContact - implementation
       */
      @Override
      public void attachView(LoginFragmentViewContract view) {
          mLoginFragmentView = view;
      }
  
      @Override
      public void detachView() {
          mLoginFragmentView = null;
      }
  
      @Override
      public void validateCredentials() {
          if(mLoginModelContract != null) {
              mLoginModelContract.login(
                      mLoginFragmentView.getUsername(),
                      mLoginFragmentView.getPassword(),
                      LoginPresenterImp.this);
          }
      }
  
      /*
       * LoginModelContract.OnLoginCompletedListener - implementation
       */
      @Override
      public void onUsernameError() {
          if(mLoginFragmentView != null) {
              mLoginFragmentView.onLoginFailed("Incorrect username");
          }
      }
  
      @Override
      public void onPasswordError() {
          if(mLoginFragmentView != null) {
              mLoginFragmentView.onLoginFailed("Incorrect password");
          }
      }
  
      @Override
      public void onSuccess() {
          if(mLoginFragmentView != null) {
              mLoginFragmentView.onLoginSuccess();
          }
      }
  }
  </code></pre>
  
  <p>Model interface</p>
  
  <pre><code>public interface LoginModelContract {
      interface OnLoginCompletedListener {
          void onUsernameError();
          void onPasswordError();
          void onSuccess();
      }
      void login(String username, String password, OnLoginCompletedListener onLoginCompletedListener);
  }
  </code></pre>
  
  <p>Model Implementation</p>
  
  <pre><code>public class LoginModelImp implements LoginModelContract {
      /* Testing Valid username and passwords */
      private static String validUsername = "steve";
      private static String validPassword = "1234";
  
      @Override
      public void login(final String username,
                        final String password,
                        final OnLoginCompletedListener onLoginCompletedListener) {
  
          boolean hasSuccess = true;
          if(TextUtils.isEmpty(username) || !username.equals(validUsername)) {
          /* TEST onUsernameError() */
              onLoginCompletedListener.onUsernameError();
              hasSuccess = false;
          }
  
          if(TextUtils.isEmpty(password) || !password.equals(validPassword)) {
          /* TEST onPasswordError() */
              onLoginCompletedListener.onPasswordError();
              hasSuccess = false;
          }
  
          if(hasSuccess) {
          /* TEST onSuccess() */
              onLoginCompletedListener.onSuccess();
          }
      }
  }
  </code></pre>
  
  <p>JUnit4 test with Mockito</p>
  
  <pre><code>public class LoginPresenterImpTest {
      private LoginFragmentViewContract mMockViewContract;
      private LoginModelContract mMockModelContract;
      private LoginModelContract.OnLoginCompletedListener mMockOnLoginCompletedListener;
      private LoginPresenterContract&lt;LoginFragmentViewContract&gt; mLoginPresenterContract;
  
      @Before
      public void setUp() throws Exception {
          mMockViewContract = Mockito.mock(LoginFragmentViewContract.class);
          mMockModelContract = Mockito.mock(LoginModelContract.class);
          mMockOnLoginCompletedListener = Mockito.mock(LoginModelContract.OnLoginCompletedListener.class);
          mLoginPresenterContract = new LoginPresenterImp(mMockModelContract);
          mLoginPresenterContract.attachView(mMockViewContract);
      }
  
      @Test
      public void shouldSuccessWithValidCredentials() {
          when(mMockViewContract.getUsername()).thenReturn("steve");
          when(mMockViewContract.getPassword()).thenReturn("1234");
  
          mLoginPresenterContract.validateCredentials();
  
          verify(mMockViewContract, times(1)).getUsername();
          verify(mMockViewContract, times(1)).getPassword();
  
          verify(mMockOnLoginCompletedListener, times(1)).onSuccess();
  
          verify(mMockOnLoginCompletedListener, never()).onPasswordError();
          verify(mMockOnLoginCompletedListener, never()).onUsernameError();
      }
  }
  </code></pre>
  
  <p>Is there any way to test this implementation?</p>
  
  <p>Many thanks for any suggestions,</p>