caipivara
1/11/2017 - 3:30 PM

Make espresso wait for all RX java calls - source: http://collectiveidea.com/blog/archives/2016/10/13/retrofitting-espresso

Make espresso wait for all RX java calls - source: http://collectiveidea.com/blog/archives/2016/10/13/retrofitting-espresso

import android.os.AsyncTask;
import android.support.test.runner.AndroidJUnitRunner;
import rx.Scheduler;
import rx.functions.Func1;
import rx.plugins.RxJavaHooks;
import rx.schedulers.Schedulers;

public class EspressoRxJUnitRunner extends AndroidJUnitRunner {

  @Override
  public void onStart() {
    Func1<Scheduler, Scheduler> asyncTaskScheduler = new Func1<Scheduler, Scheduler>() {
      @Override
      public Scheduler call(Scheduler scheduler) {
        return Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);
      }
    };

    RxJavaHooks.setOnIOScheduler(asyncTaskScheduler);
    RxJavaHooks.setOnComputationScheduler(asyncTaskScheduler);
    RxJavaHooks.setOnNewThreadScheduler(asyncTaskScheduler);

    super.onStart();
  }
}