Testing asynchronous RxJava code using Mockito
Fabio Collini
2308

Due to the AndroidSchedulers initialization sequence, the RxAndroidPlugins main thread handler needs to be set in or before @BeforeClass using this method:

RxAndroidPlugins.setInitMainThreadSchedulerHandler()

Otherwise you’ll encounter this exception:

java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked

You could actually init all of the schedulers statically before the tests are run by making ImmediateSchedulerRule a @ClassRule instead and calling these methods:

RxJavaPlugins.setInitIoSchedulerHandler(s -> immediate);
RxJavaPlugins.setInitComputationSchedulerHandler(s -> immediate);
RxJavaPlugins.setInitNewThreadSchedulerHandler(s -> immediate);
RxJavaPlugins.setInitSingleSchedulerHandler(s -> immediate);
RxAndroidPlugins.setInitMainThreadSchedulerHandler(s -> immediate);

Source: https://medium.com/@peter.tackage/overriding-rxandroid-schedulers-in-rxjava-2-5561b3d14212#b828