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.
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.
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
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”
3. Name your new mock server!
4. 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 your environment to “Github Mock Server” at the top right and try to call your mock server by clicking send. Boom!!!!
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
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.
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.
But now, run with your Mock server!
With your mock server, you can test many more cases