Android Retrofit Mock Response Tutorial

Writing test cases is a difficult task when handling of different server responses. Testing error responses can be quite problematic and your app might not cover all the different scenarios. In order to test your Android apps, one thing that normally gets frequently overlooked is the apps ability to handle different server responses. We can make the client return different responses which are stored in .JSON files within the test project. In this example, I will discuss on implementing Android retrofit mock response by custom Retrofit Client to mock  HTTP response codes. 

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “RetrofitMockResponseExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many Android devices currently support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add new dependency

Go to the build.gradle file and add the retrofit dependency in your project.

Edit Android.Manifest.xml

Open Android Manifest file and Insert internet permission in order to call API.

Add an assets folder

To create a new assets folder, you can refer to the following image.

Add a new .json file

Create folder “mock.api” in the assets folder and create another folder “user” in the “mock.api” folder. After that, create a new JSON file “login” and copy the source code below.

Create fake interceptor java class

Create a new class “FakeInterceptor” and implements Interceptor on it. The API response will make force to use the .json file from the assets.

Add a new package folder “models”

Right-click your package name and create a new package folder “models” for a retrofit response.

Create java class for Response

Create class “ResponseStatus” in the “models” package folder, this is getting the status and message of the response.

In the “models” package folder, create another class “ResponseData” is to get the JSON response data from this object. It will extend the “ResponseStatus” class.

Add another new package folder “api”

Right-click your package name and create a new package folder “api” for a retrofit request.

Create a new java class

Right-click the api folder and create a new class “RestClient”. After that insert the following code to this class. This will add the FakeInterceptor in order to use the JSON file from the assets.

Create a new interface class

In the API folder, create an interface “RestService” so that you able to call the login API according to the query and path.

 Edit activity_main.xml layout

Go to this activity_main.xml file and it will add an id in your textview.

Edit class

Open class and this class will be called the login API and display it on the screen.

Run Your Project

Lastly, you can now run your android project and view the mock json response from your devices.

(Android Retrofit Mock Response)

Source Code

(Visited 2,573 times, 3 visits today)

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *