API API Mocking and Stubbing 1 — Questions and Answers
Question 1: What is an API mock in the context of software testing?
- A real API endpoint that returns live production data
- A simulated API that mimics the behavior of a real API with predefined responses (Correct answer)
- A testing framework used for validating API response schemas
- A monitoring tool that records API traffic in real time
Correct answer: A simulated API that mimics the behavior of a real API with predefined responses
An API mock simulates the behavior of a real API by returning predefined responses, allowing testing without requiring the actual backend to be available.
Question 2: What is the primary difference between a mock and a stub in API testing?
- Stubs are only used for HTTP APIs while mocks support any protocol
- A stub returns canned responses without verifying interactions, while a mock also sets and verifies expectations about how it was called (Correct answer)
- Mocks are simpler than stubs and do not support dynamic configuration
- There is no difference; mock and stub are fully interchangeable terms
Correct answer: A stub returns canned responses without verifying interactions, while a mock also sets and verifies expectations about how it was called
A stub provides predefined responses without asserting how it was called, whereas a mock also records and verifies expectations about interactions.
Question 3: Which scenario is best suited for using an API mock during development?
- When you want to validate real API performance under heavy concurrent load
- When the third-party API is unavailable, still under development, or too costly to call repeatedly (Correct answer)
- When you need to verify the API's TLS certificate chain is valid
- When testing requires live production data that cannot be replicated offline
Correct answer: When the third-party API is unavailable, still under development, or too costly to call repeatedly
API mocking is ideal when the actual API is unavailable, under development, or when isolating the system under test from external dependencies is required.
Question 4: In test double terminology, what is a 'fake'?
- An invalid API response deliberately crafted to trigger error handling
- A working but simplified implementation that behaves correctly yet is unsuitable for production (Correct answer)
- A recorded HTTP response from a real API that is replayed during tests
- A stub that only responds to GET requests and ignores all others
Correct answer: A working but simplified implementation that behaves correctly yet is unsuitable for production
A fake is a lightweight working implementation (such as an in-memory database) that functions correctly in tests but lacks the robustness needed for production.
Question 5: What does 'service virtualization' mean in the context of API testing?
- Running API test suites inside virtual machine environments
- Creating simulated replicas of dependent services to enable testing when real services are unavailable (Correct answer)
- Virtualizing network bandwidth between microservices for performance testing
- Hosting API documentation on a cloud-based virtual server
Correct answer: Creating simulated replicas of dependent services to enable testing when real services are unavailable
Service virtualization creates simulated versions of dependent services, enabling integration testing of complex systems when real backends are unavailable or impractical to use.
Question 6: Which of the following is a primary benefit of API mocking during parallel team development?
- Mocks eliminate the need for any integration testing later in the development cycle
- Frontend and backend teams can develop and test independently against an agreed-upon API contract (Correct answer)
- Mocks consistently deliver better runtime performance than real API endpoints
- Mocks automatically generate exhaustive test cases directly from the OpenAPI specification
Correct answer: Frontend and backend teams can develop and test independently against an agreed-upon API contract
API mocks allow frontend and backend teams to work in parallel by providing a stable, agreed-upon interface before the real backend is complete.
Question 7: What is a 'spy' in test double terminology?
- A security tool that monitors and logs all network traffic between API endpoints
- A test double that records calls made to it while still delegating to the real underlying implementation (Correct answer)
- A mock configured exclusively to validate security and authorization headers
- A stub that returns different responses depending on the values of incoming request parameters
Correct answer: A test double that records calls made to it while still delegating to the real underlying implementation
A spy wraps the real implementation, recording interactions such as call count and arguments, while still invoking the actual code rather than replacing it.
What is an API mock in the context of software testing?