Fake Retrofit server response

While working on the chat app and knowing that the backend will be done in about 3-4 weeks, I decided that I will not wait that long to test my server connection and responses. Especially, because the flow of the app is pretty complex. Thus, I started to look on how to somehow mock the server response.

Then, I found out there’s an interface in okHttp3 called Interceptor that I can implement to intercept and modify the response from the server.

The created class looks like this:

I have three json objects (lines 3, 9 and 15) made as string.
I check the request URI in case I want to have multiple responses depending on the URI (line 28).
I created an array from the three objects (line 29) for the response.
I made the response (line 33) with code, body, headers etc and returned it.

The only remaining thing to do now is create my OkHttp client that I’m gonna add to retrofit:

And that’s it 🙂 Enjoy the mock server response.

Leave a Comment


Recent posts