What if you could test your app perfectly without waiting for a real server to respond?
Mock vs stub comparison in Postman - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine testing an app that talks to a slow or incomplete server. You try to check if your app works, but the server is down or returns unexpected data. You wait and wait, or guess what might happen next.
Manually testing with a real server is slow and unreliable. You can't control the server's responses easily, so tests break often. It's like calling a friend who never answers or gives confusing replies.
Mocks and stubs let you pretend the server is there and behaves exactly how you want. You create fake responses to test your app quickly and safely, without waiting or guessing.
Send request to real server and wait for response Check if response is correct
Use stub to return fixed response Use mock to check if request was made correctly
With mocks and stubs, you can test your app anytime, control all responses, and catch bugs early without relying on real servers.
When building a shopping app, you use stubs to fake product data and mocks to verify your app asks for the right products, even if the real product server is offline.
Manual testing with real servers is slow and unreliable.
Stubs provide fake data to test app behavior.
Mocks verify interactions between app and server.
Practice
mock and a stub in Postman testing?Solution
Step 1: Understand mock behavior in Postman
Mocks simulate real API behavior and can return different responses based on requests, making them dynamic.Step 2: Understand stub behavior in Postman
Stubs provide fixed, predefined responses to replace real API calls, without simulating behavior.Final Answer:
Mocks simulate API behavior with dynamic responses; stubs return fixed responses. -> Option BQuick Check:
Mock = dynamic, Stub = fixed [OK]
- Confusing mocks as fixed response tools
- Thinking stubs simulate behavior dynamically
- Believing mocks and stubs are identical
- Assuming stubs can replace mocks fully
Solution
Step 1: Identify stub creation method
Stubs are created by defining fixed example responses in a Postman collection to replace real API calls.Step 2: Eliminate other options
Mocks use dynamic rules, JavaScript simulates logic but is not stub creation, and monitors check live APIs.Final Answer:
Create a collection with fixed example responses and disable real API calls. -> Option AQuick Check:
Stub = fixed examples in collection [OK]
- Confusing mocks with stubs in creation steps
- Thinking JavaScript test scripts create stubs
- Using monitors as stubs
- Assuming dynamic responses are stubs
Solution
Step 1: Analyze mock server response
The mock server returns {"success": true} dynamically for GET /user, indicating success.Step 2: Analyze stub response effect
The stub always returns {"success": false}, a fixed failure response, which differs from mock.Final Answer:
Tests expecting success will fail because stub returns fixed failure response. -> Option AQuick Check:
Stub fixed failure ≠ mock success [OK]
- Assuming stub matches mock dynamic behavior
- Ignoring response body differences
- Thinking status code alone determines test pass
- Confusing syntax errors with logic errors
Solution
Step 1: Identify URL configuration issue
If tests receive stub responses instead of mock, likely the test requests use the stub server URL mistakenly.Step 2: Verify other options
Mock servers must be enabled; stub server down would not cause stub responses; Postman supports dynamic mocks.Final Answer:
The mock server URL is incorrectly set to the stub server URL in the tests. -> Option CQuick Check:
Wrong URL causes stub responses [OK]
- Assuming mock server is disabled by default
- Believing stub server downtime causes stub responses
- Thinking Postman lacks dynamic mock support
- Ignoring URL configuration in tests
Solution
Step 1: Identify need for dynamic and fixed responses
Testing different user roles requires dynamic responses for flexibility and fixed responses for stable baseline tests.Step 2: Combine mocks and stubs effectively
Mocks simulate dynamic role-based responses; stubs provide fixed responses for simple, repeatable tests.Final Answer:
Use a mock server to simulate dynamic role responses and a stub with fixed responses for basic role tests. -> Option DQuick Check:
Mocks dynamic + stubs fixed = best combo [OK]
- Using only stubs loses dynamic testing benefits
- Using only mocks may complicate simple tests
- Ignoring mocks and stubs reduces test reliability
- Testing only live API risks instability
