title: Mock Retrofit Observable<T> response in Android Unit tests
body: <p>I have an API interface and I'm testing a <code>View</code> that involves network calls.</p>
  
  <pre><code>@Config(emulateSdk = 18)
  public class SampleViewTest extends RobolectricTestBase {
  
      ServiceApi apiMock;
  
      @Inject
      SampleView fixture;
  
      @Override
      public void setUp() {
          super.setUp(); //injection is performed in super
          apiMock = mock(ServiceApi.class);
          fixture = new SampleView(activity);
          fixture.setApi(apiMock);
      }
  
      @Test
      public void testSampleViewCallback() {
          when(apiMock.requestA()).thenReturn(Observable.from(new ResponseA());
          when(apiMock.requestB()).thenReturn(Observable.from(new ResponseB());
  
          AtomicReference&lt;Object&gt; testResult = new AtomicReference&lt;&gt;();
          fixture.updateView(new Callback() {
  
              @Override
              public void onSuccess(Object result) {
                  testResult.set(result);
              }
  
              @Override
              public void onError(Throwable error) {
                  throw new RuntimeException(error);
              }
          });
  
          verify(apiMock, times(1)).requestA();
          verify(apiMock, times(1)).requestB();
  
          assertNotNull(testResult.get());
      }
  }
  </code></pre>
  
  <p>For some reason <code>apiMock</code> methods are never called and verification always fails.</p>
  
  <p>In my view I'm calling my api like this</p>
  
  <pre><code>apiV2.requestA()
      .subscribeOn(Schedulers.io())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(new Observer());
  </code></pre>
  
  <p>What am I missing here?</p>
  
  <p><strong>Update #1:</strong><br>
  After some investigation it appears that when in my implementation (sample above) I <code>observeOn(AndroidSchedulers.mainThread())</code> subscriber is not called. Still do not know why.</p>
  
  <p><strong>Update #2:</strong><br>
  When subscribing just like that <code>apiV2.requestA().subscribe(new Observer());</code> everything works just fine - mock api is called and test passes.<br>
  Advancing <code>ShadowLooper.idleMainLooper(5000)</code> did nothing. Even grabbed looper from handler in <code>HandlerThreadScheduler</code> and advanced it. Same result.</p>
  
  <p><strong>Update #3:</strong>
  Adding actual code where API is used.</p>
  
  <pre><code>public void updateView(final Callback) {
      Observable.zip(wrapObservable(api.requestA()), wrapObservable(api.requestB()),
          new Func2&lt;ResponseA, ResponseB, Object&gt;() {
              @Override
              public Object call(ResponseA responseA, ResponseB responseB) {
                  return mergeBothResponses(responseA, responseB);
              }
          }
      ).subscribe(new EndlessObserver&lt;Object&gt;() {
  
          @Override
          public void onError(Throwable e) {
              Log.e(e);
              listener.onError(e);
          }
  
          @Override
          public void onNext(Object config) {
              Log.d("Configuration updated [%s]", config.toString());
              listener.onSuccess(config);
          }
      });
  }
  
  protected &lt;T&gt; Observable&lt;T&gt; wrapObservable(Observable&lt;T&gt; observable) {
      return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
  }
  </code></pre>