My previous articles [1], [2] highlighted the importance of overriding the default Scheduler for certain RxJava operators to help ensure the validity of your tests and I also presented the Scheduler Injection pattern as a means of achieving this.

However, manually performing this can be error prone. You need to be familiar enough with the dozens of RxJava operators to know when it’s applicable and consequently, despite code reviews, mistakes can slip in and lead to flaky tests and bugs in your app.

RxLint has been around for a while and provides custom Lint checks for common RxJava mistakes. Since the recent version 1.6, …

In my previous article, I described how the RxAndroidPlugins APIs for RxJava 2 can be used to override Schedulers to create a suitable execution environment for JVM-based unit tests.

As I hinted, that’s not your only option. In this follow-up, I will describe another possibility; Scheduler Injection and we’ll see how it compares to using the plugins approach.

If you are familiar with RxJava 1.x, but haven’t yet started with RxJava 2.x; don’t worry, I’ll mention some specifics from 2.x, but the principles described are still generally applicable to both versions.

Scheduler Injection?

Scheduler Injection is simply supplying the desired Scheduler instance to your operators, rather than letting them run on their defaults. …

RxAndroid now supports RxJava 2, and with it, a new set of APIs for overriding Schedulers. In this post, I’ll explain these APIs, their motivation and show you how to use them to address a common Android unit testing problem.

But first, a bit of background

In both RxJava 1 and 2, Schedulers are used to control the concurrency and timing of operations. They are applied primarily using the observeOn and subscribeOn operators. Additionally, some other operators, such as delay or debounce are executed on specific Schedulers by default, but are also overloaded to allow you to use an alternative Scheduler.

RxAndroid defines its own Scheduler through AndroidSchedulers.mainThread(). This Scheduler can be used to meet the Android requirement that all UI actions are performed on Android’s main thread. …


Peter Tackage


Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store