
How To Better Unit Testing ?
Demonstration with a simple networking app
As an Android developer, this year was really exciting and full of good news if creating robust and maintainable apps matters to you. The Architecture Components was really a game changer !
In this post, I will show you how to proper Unit Testing a simple existing Android app: RetroKotlin.
🖌A Little Word About The App
RetroKotlin, obviously written in Kotlin, is really simple: when MainActivity starts, a RxJava2 stream inside a ViewModel (UserViewModel) will fetch Jake Wharton’s Github data (represented by User model) before showing it through the screen, thanks to LiveData.

I’ve also used Dagger 2 to provide all the dependency injection, ViewModel injection included (ViewModelFactory). You can find the AppComponent and the different modules inside the di/ package.
To fetch data from Github API, I’ve naturally used Retrofit 2 and Gson.
Before continue reading, I highly recommend you to take a look at the app source code to understand how it works (at least MainActivity).
💡 WHAT Should We Unit Test ?
As you will have noticed, the stream fetching Github user’s data is located inside the UserViewModel. Because it’s the main feature of our app (getting Jake Wharton’s data from Github API), we should be able to unit testing it.
So the main idea will be, during our unit tests, to :
- Call the ViewModel UserViewModel
- Execute its unique stream getUser() that will fetch Jake Wharton’s Github profile information.
- Mock the API response
- Check the result through the User LiveData model.
🤔 HOW Should We Unit Test ?
🤞Preparing the ground…
First, we will use MockWebServer library to mock the API response. We will configure it inside an abstract class that we will call BaseTest.
Now, the tricky part is to tell our app, during Unit Tests ONLY, to NOT fetching data from real Github API, but instead using a mocked API response from MockWebServer.

To achieve that properly, we will have to tell Dagger 2 to inject different dependencies in order to custom app behavior during unit tests (that one of the main purpose of DI !).
Like so, we will have to tell him that all Rx streams have to work on the main thread during tests. In this way, we have created a test module (TestRxJavaModule) that provides a unique scheduler for both onSubscribe() and onObserve() methods : AndroidSchedulers.mainThread()
We also have created a TestAppComponent to inject custom dependencies during our unit tests, including the previous TestRxJavaModule.
Next, inside our previous BaseTest class, we have configured all the dependencies we will use during our tests.
Note that we inject TestRxJavaModule instead of RxJavaModule and we provide to NetModule the URL generated by the MockWebServer.
That’s good ! Now Dagger 2 will provide us different dependencies for our unit tests.

💪 Welcome aboard, sailor!
Let’s talk about unit test itself. We want to check if everything is alright when getting user (when success AND when fails).
First, we will have to create a class extending from BaseTest :
We have created the UserUnitTest class, which will contain all our future unit tests.
The most important code here is the line 6 with InstantTaskExecutorRule. It will tell JUnit to force tests to be executed synchronously, especially when using Architecture Components.
We can now finally add our unit tests… 😉
Those tests are cleans, aren’t they ?
Before each test, we use the previously created mockHttpResponse() method that will configure and mock the Github API response from a json file located inside resources/ directory.
Next, we execute our stream getUser() located inside our ViewModel UserViewModel. This stream will be executed synchronously thanks to our previous injections defined inside the TestRxJavaModule.
Finally, we check each LiveData variables (user, isLoading & errorMessage) to verify if it’s content is correct.
That’s all. Our RetroKotlin app and its main feature getUser() is unit tested now, without touching anything on production code.
Unit Testing with Architecture Components is really accessible if we use the right tools. MockWebServer will allow us to easily mock and test HTTP request (thanks Jake Wharton !).
ViewModel and LiveData change the way we design our apps and make testing more comfortable and safe. Using any DI framework like Dagger 2 will make our code more modulable and easily testable.
Hope this post helped you ! You can find the entire code on Github : https://github.com/PhilippeBoisney/RetroKotlin