Mocking JSON responses from Retrofit calls and make them RxJava/RxKotlin Observable for unit testing
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.
- Chaining and Running Retrofit calls in parallel with RxJava
- Handling different status codes from Retrofit calls differently with RxJava
- 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