title: How to verify no interactions on RxJava2 Observable using Mockito
body: <p>Observable is final class, so we can't create mocks for it to verify or capture any interactions performed on it.</p>
  
  <p>And Observable's <code>test()</code>, TestSubscriber also does not provide any such interaction assertion techinque</p>
  
  <p>I have created a generic method for checking cached data before loading from network</p>
  
  <pre><code>/**
   * General parametrized method for loading data from service while checking connection
   * and respecting reload state
   */
  private &lt;T&gt; Observable&lt;T&gt; getData(Observable&lt;T&gt; dataSource, String key, boolean reload) {
      T cachedData = (T) cacheModel.getValue(key);
  
      // Do not use cache if reloading
      if(!reload &amp;&amp; cachedData != null)
          return Observable.just(cachedData);
  
      if(!utilModel.isConnected()) {
          return Observable.error(new Throwable(Constants.NO_NETWORK));
      }
  
      return dataSource
          .doOnNext(data -&gt; cacheModel.saveObject(key, data))
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread());
  }
  </code></pre>
  
  <p>And using it as follows:</p>
  
  <pre><code>@Override
  public Observable&lt;User&gt; getOrganiser(boolean reload) {
      return getData(eventService.getUser(authorization), ORGANIZER, reload);
  }
  </code></pre>
  
  <p>Before, I was not even calling <code>eventService.getUser(...)</code> to get the observable, so I could test if it was never being called, but now, as I have to pass it to the template method, I need to call it but verify if cache is present, it is never interacted with.</p>
  
  <p>This is my previous test, which is obviously failing now</p>
  
  <pre><code>@Test
  public void shouldLoadOrganizerFromCache() {
      // Clear cache
      objectCache.clear();
  
      User user = new User();
      objectCache.saveObject(RetrofitEventRepository.ORGANIZER, user);
  
      // No force reload ensures use of cache
      Observable&lt;User&gt; userObservable = retrofitEventModel.getOrganiser(false);
  
      userObservable.test().assertNoErrors();
      userObservable.test().assertValue(user);
  
      verify(eventService, never()).getUser(auth);
  }
  </code></pre>