API Mocking and Stubbing Flashcards
7 cards from real API practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 API Mocking and Stubbing flashcards as text
What is 'contract testing' in API testing?
Answer: Verifying that an API provider and its consumers agree on the expected structure and behavior of the API interface
Contract testing verifies that both the API provider and its consumers honor a shared contract defining the expected request/response format, ensuring compatibility without full end-to-end integration tests.
Which tool is specifically designed for consumer-driven contract testing of APIs?
Answer: Pact
Pact is a consumer-driven contract testing framework that allows consumers to publish their expected API interactions and providers to verify they fulfill those expectations.
What is the primary risk of using overly simplified API mocks in automated tests?
Answer: Tests may pass against the mock but fail in production if the mock does not accurately reflect real API behavior
If mocks don't accurately replicate real API behavior—including edge cases, error conditions, and data formats—tests provide false confidence and real integration failures may go undetected.
In API mocking, what does 'chaos engineering' applied to mock servers involve?
Answer: Deliberately configuring mocks to return errors, timeouts, or malformed responses to verify system resilience
Applying chaos engineering through mock servers means intentionally injecting failures such as HTTP 500 errors, network delays, or malformed payloads to confirm the client handles adverse conditions gracefully.
When should you prefer integration testing against real APIs over mock-based testing?
Answer: During final acceptance testing when verifying actual compatibility with the real third-party service is critical
Real API integration tests are essential during acceptance testing to validate that your code works correctly with the actual service, even though mocks are preferred for most unit and integration tests.
What is 'stateful mocking' in API testing?
Answer: Configuring a mock server to change its responses based on the sequence or history of previous requests received
Stateful mocking allows mock servers to track state across multiple requests and return different responses based on prior interactions, simulating real-world API workflows like order processing.
According to the 'test pyramid' principle, how should API mock usage be distributed across testing levels?
Answer: Use mocks extensively in fast unit tests to isolate components, less so in integration tests, and minimally in end-to-end tests
The test pyramid recommends heavy mock usage in the large base of fast unit tests, reduced mocking in integration tests, and minimal mocking in the small top layer of end-to-end tests that validate real-world behavior.