Testing RxJava Observable with TestSubscriber

RxJava gained so much popularity this year, I think it’s kinda obvious because it helps us to make our code composable. I won’t talk much about how to use it but, if you haven’t heard/tried it yet I recommend you to check it out here or you can check really nice tutorial by Dan Lew here (For RxJava 2.0 you can check vogella’s tutorial).

Testing RxJava code can be intimidating because all operations in RxJava is asynchronous by default. Let’s check several ways how we can test it.

Naive way

We can declare a global variable and set it inside the subscriber and then assert that the variable is not null.

Ending Words

There are several ways to do testing in RxJava, but I recommend you to use TestSubscriber because it will give you more feature, flexibility, better error message and it’s also the official way to test an Observable in RxJava.
Thanks for reading and if you have any question you can write comment below or reach me via twitter @niko_yuwono.