Mocking JSON responses from Retrofit calls and make them RxJava/RxKotlin Observable for unit testing

Aung Thiha
1 min readAug 3, 2019

--

Beauty of my hometown, Mandalay

Previously, I wrote about Handling different status codes from Retrofit calls differently with RxJava. This post is kind of part 3 but you don’t really need to read part 1 or 2 first to understand this but if you’re interested, please do check my previous posts.

This is one of the posts from the series I’m writing.

  1. Chaining and Running Retrofit calls in parallel with RxJava
  2. Handling different status codes from Retrofit calls differently with RxJava
  3. Mocking JSON responses from Retrofit calls and make them Observable for unit testing (You’re here)

Add mock dependency in your gradle

dependencies {
testImplementation "org.mockito:mockito-inline:{latest_version_here}"
}

One thing to note is in my previous posts, I used Observable. In this example, I’m using Single. If you haven’t checked the difference, you can google the difference between Observable, Single, Maybe, Completable and Flowable.

Now, Let’s look how we test a mock retrofit

I hope the comments and the code is clear enough. If you need any explanation, please let me know. I’ll be happy to add.

Thank you for reading

--

--

Aung Thiha

Android | Jetpack Compose | A/B Testing | TDD | CI/CD | SOLID | Clean Architecture | Design Patterns | Mentorship | Recruitment