How Android developer mocking response data from Server using Postman?

A-Rohim Sama
5 min readJun 23, 2020

--

As a mobile developer, we have to handle every response from the server to make sure our lovely application works properly.

To test every response from the Server we have to make mocking data in our project locally this might be consumed your time a lot and we have to change our mocking data when the server changing the response if we have Android, iOS and Web this task will be tripled.

For testers you can test the application super easy, you don’t have to ask Back end team to change the response every test cases and wait a few hours for testing (T_T), Therefore once your application is easy to test the quality surely higher! \0/

Once you have your Mock server you can make UI testing for you regression testing easily.

Perfect!!!

This article I will show you how to use Postman as a Mock server where you can easily change the response and you also can make UI testing consistently

We will fetch some Public GitHub repositories from GitHub and use the only name and description keys to be easy in this sample project.

What is Postman?

Postman is a collaboration platform for API development. Postman’s features simplify each step of building an API and streamline collaboration so you can create better APIs — faster. www.postman.com

How to create a Mocking server using Postman?

I assumed you already download and signed in, you can download Postman here

  1. Create a new Mock server by clicking on New at the left top and choose Mock Server

2. Add /repositories path and Response Body “Mock body sample”

3. Name your lovely mock server!

4. And create the mock server you will see your mock URL like this.

5. Expand the “Github Mock Server” collection to see your API request and don’t forget to change you Environment to “Github Mock Server” at the top right and try to call your mock server by clicking send Boom!!!!

Congratulation! you have your own Mock server!!!

Time to put your magic on your mock server!

From here you can update the response in this sample project we will add this response

  1. You can update the default response by click on Examples and choose Default.

2. From here you can change your response, status code and don’t forget to add “Content-Type: application/json” into your response headers to tell the response format to the someone who calls this API

3. Save your config and try to call API again you will see your response data like this.

Yes!, all done for the mock server!

Let’s try the mock server on our Android Project.

You need mock build type to get your mock server URL separately from the Production!

There are many ways to do this, one way that it works well with both android library module and android application module.

  1. Create a common-properties.gradle and save it to the root of the project this way you can reuse the config across modules

2. Import common-properties.gradle into your app/build.gradle

apply from '../common-properties.gradle'
...
android {
...

3. Re-sync and switch your Build variant to mock and rebuild your project, after you re-build your project successfully you will see BASE_API_URL in your auto-generated BuildConfig file.

4. Now you can access your base API URL everywhere in your app.

BuildConfig.BASE_API_URL

Run Android App on Production!

With this way, you can test only when API Server response success and no internet connection!

Production server!
No internet connection

But!!!, with the mock server!

with the mock server, you can test a lot more cases

You can mock some items
You can mock an empty list
You can mock 500 Internal server error

You can explore code sample here https://github.com/arohim/SampleMockServerWithPostman

Conclusions

  1. You don’t have to make some mocking data inside the front-end projects, you can use the mock server many places.
  2. One place changing.
  3. Easy to communicate between Back-end and Front-end with an amazing tool from Postman to make API documents.
  4. Testers can test all the expected test cases.
  5. And the most important is this can speed up the development time because you don’t have to wait for Back-end team mock the data for testing any more.

Thank you for reading my article if you guys have other ways please share me I love to learn new ways (^_^)

Resources

--

--