title: Unit testing android application with retrofit and rxjava
body: <p>I have developed an android app that is using retrofit with rxJava, and now I'm trying to set up the unit tests with Mockito but I don't know how to mock the api responses in order to create tests that do not do the real calls but have fake responses.</p>
  
  <p>For instance, I want to test that the method syncGenres is working fine for my SplashPresenter. My classes are as follow:</p>
  
  <pre><code>public class SplashPresenterImpl implements SplashPresenter {
  
  private SplashView splashView;
  
  public SplashPresenterImpl(SplashView splashView) {
      this.splashView = splashView;
  }
  
  @Override
  public void syncGenres() {
      Api.syncGenres(new Subscriber&lt;List&lt;Genre&gt;&gt;() {
          @Override
          public void onError(Throwable e) {
              if(splashView != null) {
                  splashView.onError();
              }
          }
  
          @Override
          public void onNext(List&lt;Genre&gt; genres) {
              SharedPreferencesUtils.setGenres(genres);
              if(splashView != null) {
                  splashView.navigateToHome();
              }
          }
      });
  }
  }
  </code></pre>
  
  <p>the Api class is like:</p>
  
  <pre><code>public class Api {
      ...
      public static Subscription syncGenres(Subscriber&lt;List&lt;Genre&gt;&gt; apiSubscriber) {
          final Observable&lt;List&lt;Genre&gt;&gt; call = ApiClient.getService().syncGenres();
          return call
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(apiSubscriber);
      }
  
  }
  </code></pre>
  
  <p>Now I'm trying to test the SplashPresenterImpl class but I don't know how to do that, I should do something like:</p>
  
  <pre><code>public class SplashPresenterImplTest {
  
  @Mock
  Api api;
  @Mock
  private SplashView splashView;
  
  @Captor
  private ArgumentCaptor&lt;Callback&lt;List&lt;Genre&gt;&gt;&gt; cb;
  
  private SplashPresenterImpl splashPresenter;
  
  @Before
  public void setupSplashPresenterTest() {
      // 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 reference to the class under test
      splashPresenter = new SplashPresenterImpl(splashView);
  }
  
  @Test
  public void syncGenres_success() {
  
      Mockito.when(api.syncGenres(Mockito.any(ApiSubscriber.class))).thenReturn(); // I don't know how to do that
  
      splashPresenter.syncGenres();
      Mockito.verify(api).syncGenres(Mockito.any(ApiSubscriber.class)); // I don't know how to do that
  
  
  
  }
  }
  </code></pre>
  
  <p>Do you have any idea about how should I mock and verify the api responses? 
  Thanks in advance!</p>
  
  <p>EDIT:
  Following @invariant suggestion, now I'm passing a client object to my presenter, and that api returns an Observable instead of a Subscription. However, I'm getting a NullPointerException on my Subscriber when doing the api call. The test class looks like:</p>
  
  <pre><code>public class SplashPresenterImplTest {
  @Mock
  Api api;
  @Mock
  private SplashView splashView;
  
  private SplashPresenterImpl splashPresenter;
  
  @Before
  public void setupSplashPresenterTest() {
      // 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 reference to the class under test
      splashPresenter = new SplashPresenterImpl(splashView, api);
  }
  
  @Test
  public void syncGenres_success() {
      Mockito.when(api.syncGenres()).thenReturn(Observable.just(Collections.&lt;Genre&gt;emptyList()));
  
  
      splashPresenter.syncGenres();
  
  
      Mockito.verify(splashView).navigateToHome();
  }
  }
  </code></pre>
  
  <p>Why am I getting that NullPointerException? </p>
  
  <p>Thanks a lot!</p>