Mock server limitations in Postman - Build an Automation Script
const mockServerUrl = pm.environment.get('mockServerUrl'); // Test GET /user pm.test('GET /user returns 200 and expected JSON', async () => { const getResponse = await pm.sendRequest({ url: `${mockServerUrl}/user`, method: 'GET' }); pm.expect(getResponse).to.have.property('code', 200); pm.expect(getResponse.json()).to.deep.equal({"id": 1, "name": "John Doe"}); }); // Test POST /user unsupported method pm.test('POST /user returns error status for unsupported method', async () => { const postResponse = await pm.sendRequest({ url: `${mockServerUrl}/user`, method: 'POST', body: { mode: 'raw', raw: JSON.stringify({"id": 2, "name": "Jane"}) } }); pm.expect(postResponse.code).to.be.oneOf([404, 405]); });
This Postman test script uses pm.sendRequest to send HTTP requests to the mock server URL stored in an environment variable.
First, it sends a GET request to /user and asserts the response status code is 200 and the JSON body matches the expected user data.
Second, it sends a POST request to the same endpoint, which is unsupported by the mock server, and asserts the response status code is either 404 or 405, indicating the method is not allowed.
Using pm.test groups assertions clearly, and pm.expect checks the response properties. This approach verifies mock server limitations on supported HTTP methods.
Now add data-driven testing with 3 different user IDs for GET /user endpoint
Practice
Solution
Step 1: Understand what mock servers do
Mock servers simulate API responses without running real backend code or accessing databases.Step 2: Identify the limitation
Since mock servers only return fixed responses, they cannot execute real logic or database queries.Final Answer:
They cannot execute real backend logic or database queries. -> Option AQuick Check:
Mock servers = fixed responses only [OK]
- Thinking mock servers can update real databases
- Assuming mock servers adapt responses dynamically
- Believing mock servers handle unlimited traffic without limits
Solution
Step 1: Review Postman mock response format
Postman mock responses use JSON with keys like "status" and a stringified JSON body.Step 2: Check each option
{ "status": 200, "body": "{\"message\": \"Success\"}" }uses correct keys and stringifies the body JSON properly. Others use incorrect keys or formats.Final Answer:
{ "status": 200, "body": "{\"message\": \"Success\"}" } -> Option CQuick Check:
Correct keys and stringified body ={ "status": 200, "body": "{\"message\": \"Success\"}" }[OK]
- Using unquoted keys in JSON
- Not stringifying the body JSON
- Using wrong key names like 'code' or 'statusCode'
{ "status": 404, "body": "{\"error\": \"Not Found\"}" }What will be the HTTP status code and response body when the mock server is called?
Solution
Step 1: Read the mock response definition
The mock response sets status to 404 and body to a JSON string with error message.Step 2: Determine the actual response
When called, the mock server returns status 404 and the body parsed as JSON {"error": "Not Found"}.Final Answer:
Status 404 with body {"error": "Not Found"} -> Option DQuick Check:
Status and body match mock setup = Status 404 with body {"error": "Not Found"} [OK]
- Assuming default 200 status code
- Ignoring the body content
- Confusing status 404 with 500
Solution
Step 1: Understand mock server behavior
Postman mock servers return fixed responses based on saved examples; they do not run logic or read parameters.Step 2: Analyze the symptom
Receiving the same response regardless of parameters matches fixed response behavior, not a connectivity or config issue.Final Answer:
Mock servers return fixed responses and do not process request parameters. -> Option AQuick Check:
Fixed response = no parameter processing [OK]
- Assuming mock servers run backend logic
- Blaming network or server downtime incorrectly
- Expecting dynamic responses without scripting support
Solution
Step 1: Recall mock server capabilities
Postman mock servers return fixed responses from saved examples and do not run dynamic logic or scripts.Step 2: Understand error simulation approach
To test different errors, you must create multiple saved examples with fixed error responses; dynamic error generation is not possible.Final Answer:
Mock servers cannot simulate dynamic errors based on request content; you must create separate examples for each error. -> Option BQuick Check:
Fixed examples needed for each error scenario [OK]
- Expecting dynamic error generation from mock servers
- Assuming mock servers connect to real databases
- Thinking mock servers run runtime scripts
Ready to prove it?
Pass the test and this pattern moves unknown
