title: Unit testing clients of Observables
body: <p>I have the following method <code>go()</code> I'd like to test:</p>
  
  <pre><code>private Pair&lt;String, String&gt; mPair;
  
  public void go() {
      Observable.zip(
              mApi.webCall(),
              mApi.webCall2(),
              new Func2&lt;String, String, Pair&lt;String, String&gt;&gt;() {
                  @Override
                  public Pair&lt;String, String&gt; call(String s, String s2) {
                      return new Pair(s, s2);
                  }
              }
      )
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new Action1&lt;Pair&lt;String, String&gt;&gt;() {
                  @Override
                  public void call(Pair&lt;String, String&gt; pair) {
                      mApi.webCall3(pair.first, pair.second);
                  }
              });
  }
  </code></pre>
  
  <p>This method uses <code>Observable.zip()</code> to execute to http requests asynchronously, and merge them together in one <code>Pair</code>. In the end, another http request is executed with the result of these previous requests.</p>
  
  <p>I'd like to verify that calling the <code>go()</code> method makes the <code>webCall()</code> and <code>webCall2()</code> requests, followed by the <code>webCall3(String, String)</code> request. Therefore, I'd like the following test to pass (using Mockito to spy the <code>Api</code>):</p>
  
  <pre><code>@Test
  public void testGo() {
      /* Given */
      Api api = spy(new Api() {
          @Override
          public Observable&lt;String&gt; webCall() {
              return Observable.just("First");
          }
  
          @Override
          public Observable&lt;String&gt; webCall2() {
              return Observable.just("second");
          }
  
          @Override
          public void webCall3() {
          }
      });
  
      Test test = new Test(api);
  
      /* When */
      test.go();
  
      /* Then */
      verify(api).webCall();
      verify(api).webCall2();
      verify(api).webCall3("First", "second");
  }
  </code></pre>
  
  <p>However when running this, web calls are executed asynchronously, and my test executes the assertion before the subscriber is done causing my test to fail.</p>
  
  <p>I have read that you can use <code>RxJavaSchedulersHook</code> and <code>RxAndroidSchedulersHook</code> to return <code>Schedulers.immediate()</code> for all methods, but this results in the test running indefinitely.</p>
  
  <p>I am running my unit tests on a local JVM.</p>
  
  <p>How can I achieve this, preferably without having to modify the signature of <code>go()</code>?</p>