Mocking Server Response Data with Postman on Android

Here is an easy way to test your application in every scenario using Postman Mock Server


As a mobile developer, we have to handle every response from the server to make sure our beautiful applications work properly.

To test every response from the server, we often have to mock data directly from our project locally -- this consumes a lot of time considering we also have to change our data as the server changes responses. If we have Android, iOS, and Web this task will be tripled.

With Postman Mock Server,  you can test the application easily and you don’t have to ask the backend team to customize the response for every possible test case while you wait a few hours for QA (T_T). Test iterations are shorter and test quality is higher! \0/

Once you have your mock server you can focus on UI regression testing.

Perfect!!!
In this article I will show you how to use Postman as a mock server where you can easily change the response so that you can consistently test the UI.

We will fetch some public GitHub repositories and use only name and description keys 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 assume you already download and signed in, if not, 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 the /repositories path and Response Body “Mock body sample”
image.png 27.15 KB

3. Name your new mock server!

4. Create the mock server. You will see your mock URL like this.
image.png 36.24 KB

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

Congratulations! you have your own Mock server!!!


Time to put some magic into your mock server!
From here you can change your response in this sample project. We will add this response data to our mock server
[
  {
    "id": 1,
    "name": "Android-Test1",
    "description": "Android-Test1 description"
  },
  {
    "id": 2,
    "name": "Android-Test2",
    "description": "Android-Test2 description"
  },
  {
    "id": 3,
    "name": "Android-Test3",
    "description": "Android-Test3 description"
  },
  {
    "id": 4,
    "name": "Android-Test4",
    "description": "Android-Test4 description"
  }
]

  1. You can update the default response by clicking on Examples and choosing Default.


2. Next, you can change your response, status code and make sure to add “Content-Type: application/json” to your response headers to identify the response format to consumers of your API.
3. Save your config and call the API again. You should see your response data like this:


Yes! You are done with your mock server!



Now, let’s try our mock server on our Android Project.

You need a mock build type to separate the mock server URL from the Production.
There are many ways to do this. Here is one way that works well with both the Android library module and the 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.
android.buildTypes {
    mock {
        buildConfigField "String", "BASE_API_URL", "\"https://c77b2784-b60b-491b-af7e-273c356ef4a2.mock.pstmn.io\""
    }
    debug {
        buildConfigField "String", "BASE_API_URL", "\"https://api.github.com/\""
    }
    release {
        buildConfigField "String", "BASE_API_URL", "\"https://api.github.com/\""
    }
}

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!
In this way, you can test only two cases; API Server response success and no internet connection.

Production server!
No internet connection
But now, run with your Mock server!
With your mock server, you can test many more cases

You can mock response items
You can mock an empty list
You can mock 500 Internal server error
You can explore the code sample here: https://github.com/arohim/SampleMockServerWithPostman

Conclusions
  1. With the mock server, you can mock any response and any code status that you want to test easily, and it improves your test quality.
  2. You don’t have to make mock data inside the front-end projects, you can use the mock server and re-use it anywhere.
  3. You have a single source of information.
  4. Easy to communicate between Back-end and Front-end with an excellent tool from Postman to make API documents.
  5. Testers can cover all of the expected test cases.
  6. Most importantly, this can speed up the development because you don't have to wait for the Back-end team to mock the data for testing anymore.

Thank you for reading my article! If you have other suggestions please share me - I love learning new options (^_^)


Resources

Like 4 likes
Him Sama
Share:

Join the conversation

This will be shown public
All comments are moderated

Get our stories delivered

From us to your inbox weekly.