Mock Responses with OkHttp & Retrofit

The Why?

  1. Our tests should run in isolation
  2. Our tests should not be affected by external factors
  3. Our tests conditions are controllable

A flaky test is a test which could fail or pass for the same configuration.

Setup 🔨


Explore 🛫

  • Mocking Responses
  • Throttling for bodies
  • Throttling for headers
  • Many many more

Create the mock web server 🏗

val mockWebServer = MockWebServer()

Mocking a response


Basic setup for unit tests

class YourTest {
lateinit var mockWebServer: MockWebServer
fun setUp() {
mockWebServer = MockWebServer()
fun tearDown() {

MockWebServer with Retrofit

Retrofit retrofit = new Retrofit.Builder()
public Builder baseUrl(String baseUrl) {
checkNotNull(baseUrl, "baseUrl == null");
return baseUrl(HttpUrl.get(baseUrl));
  • Production
  • Tests

The Transformation


class OurApi(
private val baseUrl: String = ""
) {
private val retrofit = Retrofit.Builder()


class OurApi(
private val baseUrl: HttpUrl = HttpUrl.get("")
) {
private val retrofit = Retrofit.Builder()

Bonus 🎉

  1. Create a folder under the following path:~/${MODULE}/src/test/resources
  2. Place there the responses as JSON files such as error.json
  3. From your unit test read the file as a String and pass to the mock web server instance

Read JSON files as Strings

object FileUtils {
fun readTestResourceFile(fileName: String): String {
val fileInputStream = javaClass.classLoader?.getResourceAsStream(fileName)
return fileInputStream?.bufferedReader()?.readText() ?: ""
// pass to mock web server
val response = MockResponse()



Android Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store