Instrumentation Tests with Dagger2, Retrofit2, RxJava2 and MockWebServer — Android

Mohammed Audhil
AndroidPub
Published in
3 min readJul 4, 2019

It has been a while since I wrote a blog, I got some time to write about UI testing, which I found useful. When I started writing test cases, I was sure to use DI, mock server response, it took a while to come with a fully functional flow. I’ll share it here step-by-step.

Image Courtesy: Google

Refer my blog for Unit tests in Android

NOTE: Before starting, if you are not familiar with Dagger2, Retrofit2, RxJava2 and Kotlin. Pls, Stop! there are plenty of resources available on the internet, have some knowledge, then continue with this flow.

Step 0: App Demo

Let’s begin,

It’s a simple app, once you launch, it’ll hit an API call based on the success or failure, we’ll update the text view.

Success & Failure response — network call

Step 1: MockWebServer

As the name suggests, it mocks your web server. It’s a handy tool from Square.

Step 2: Dagger2

I’ve made a function getAppComponent() in app’s Application class to return DaggerAppComponent,

Step 3: Create Dagger2 Module, TestApplicationModule.kt

Mohammed Audhil
AndroidPub

Software Engineer @Blackhawk_Networks | more: https://github.com/Audhil (Pray. Eat. Code. Sleep.)