Mockfit: A simple way to mock retrofit response in Android

Mahdi Javaheri
Logicbase
Published in
2 min readApr 17, 2021
Photo by Fachry Aiko on Unsplash

Writing app when api is not implemented yet, has it’s challenges. You may decide to create a fake api server and consume it in your android project.
Or you just instantiate objects with default data to be able to test your UI and functionalities. What if server is down when you have implemented with real api and removed all previous objects?

A nice and clean approach without cumbersome activities mentioned above would be having a mock layer above your network api which will remove boilerplate codes and handle the effort for intercepting between server and your app.

With Mockfit you just define your network response in json files and annotate your api it’s location! if you want to see code checkout the link below:

How does it work

Mockfit is using retrofit library, make sure to add it’s dependency and configure in your project.

Create json file of your api response lets say picsum_list.jsonand put it in assets/mock_jsondirectory

Define your api interface and annotate endpoint with @mock("picsum_list.json") so that Mockfit can generate config for you

Add MockfitInterceptor to retrofit configuration

That’s it now you can see your mock data in UI:

You can reach me at Twitter, LinkedIn or Github if you have any questions.
If you find any possible mistake or just have better ideas, feel free to comment and share your thoughts.

--

--