title: Testing asynchronous RxJava code - Android
body: <p>I am in the process of getting to know RxJava. I started using it in one of my personal apps and would like to unit test the code but having some difficulties and would like some help. </p>
  
  <p>The scenario is simple.</p>
  
  <ol>
  <li>I get <code>UserInfo</code> object by making a REST call</li>
  <li>If the retuned <code>UserInfo</code> object is not null return <code>true</code> else return <code>false</code>.</li>
  </ol>
  
  <p>For the above scenario my RxJava code looks like this</p>
  
  <pre><code>public LiveData&lt;Boolean&gt; doesUserExists(String userName) {
      UserExistsObserver observer= new UserExistsObserver ();
      getUserInfo(userName).subscribeWith(subscriber);
      disposable.add(observer);
      return userExists;
  }
  
  public Observable&lt;Boolean&gt; getUserInfo(String userName) {
      return repository.getUserInfo(userName)
              .flatMap(new Function&lt;UserInfo, Observable&lt;Boolean&gt;&gt;() {
                  @Override
                  public Observable&lt;Boolean&gt; apply(UserInfo userInfo) throws Exception {
                        return Observable.just(userInfo != null);
                  }
              })
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread());
  }
  </code></pre>
  
  <p>So I wanted to write a simple unit test to check if getUserInfo() returns correct boolean value or not. Below is my unit test.</p>
  
  <pre><code> @Test
  public void getUserInfo_returns_true(){
      UserInfo userInfo = new UserInfo(); //Dummy data - non null userInfo object
      when(repository.getUserInfo("username")).thenReturn(Observable.just(userInfo));
  
      TestObserver&lt;Boolean&gt; testObserver = new TestObserver&lt;&gt;();
      //the flatMap operator should return true since userInfo is not null
      viewModel.getUserInfo("username").subscribeWith(testObserver); 
      testObserver.assertValue(true);
  }
  </code></pre>
  
  <p>And below is my log</p>
  
  <pre><code>java.lang.AssertionError: Expected: true (class: Boolean), Actual: [] (latch = 1, values = 0, errors = 0, completions = 0)
  
  at io.reactivex.observers.BaseTestConsumer.fail(BaseTestConsumer.java:163)
  at io.reactivex.observers.BaseTestConsumer.assertValue(BaseTestConsumer.java:328)
  at com.ik.githubbrowser.search_user.SearchUserViewModelTest.getUserInfo_returns_true(SearchUserViewModelTest.java:51)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
  at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
  at org.junit.rules.RunRules.evaluate(RunRules.java:20)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
  at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
  at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  
  java.lang.NullPointerException
  at io.reactivex.android.schedulers.HandlerScheduler$HandlerWorker.schedule(HandlerScheduler.java:70)
  at io.reactivex.Scheduler$Worker.schedule(Scheduler.java:272)
  at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.schedule(ObservableObserveOn.java:161)
  at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.onNext(ObservableObserveOn.java:119)
  at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.onNext(ObservableSubscribeOn.java:58)
  at io.reactivex.internal.operators.observable.ObservableScalarXMap$ScalarDisposable.run(ObservableScalarXMap.java:248)
  at io.reactivex.internal.operators.observable.ObservableScalarXMap$ScalarXMapObservable.subscribeActual(ObservableScalarXMap.java:164)
  at io.reactivex.Observable.subscribe(Observable.java:10903)
  at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
  at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
  at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
  at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
  
  Exception in thread "RxCachedThreadScheduler-1" java.lang.NullPointerException
  at io.reactivex.android.schedulers.HandlerScheduler$HandlerWorker.schedule(HandlerScheduler.java:70)
  at io.reactivex.Scheduler$Worker.schedule(Scheduler.java:272)
  at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.schedule(ObservableObserveOn.java:161)
  at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.onNext(ObservableObserveOn.java:119)
  at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.onNext(ObservableSubscribeOn.java:58)
  at io.reactivex.internal.operators.observable.ObservableScalarXMap$ScalarDisposable.run(ObservableScalarXMap.java:248)
  at io.reactivex.internal.operators.observable.ObservableScalarXMap$ScalarXMapObservable.subscribeActual(ObservableScalarXMap.java:164)
  at io.reactivex.Observable.subscribe(Observable.java:10903)
  at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
  at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
  at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
  at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
  
  Process finished with exit code -1
  </code></pre>