KEMBAR78
Mocking Vs Stubbing in API Testing + Interview Mock | PDF | Software Engineering | Computing
0% found this document useful (0 votes)
33 views5 pages

Mocking Vs Stubbing in API Testing + Interview Mock

The document compares Mocking and Stubbing in API testing, highlighting their definitions, use cases, verification methods, and implementation complexities. It also provides six interview questions with structured STAR format answers, illustrating practical applications of both techniques in various testing scenarios. Key tools mentioned include Mockito for mocking and WireMock for stubbing.

Uploaded by

backupreneemoura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Mocking Vs Stubbing in API Testing + Interview Mock

The document compares Mocking and Stubbing in API testing, highlighting their definitions, use cases, verification methods, and implementation complexities. It also provides six interview questions with structured STAR format answers, illustrating practical applications of both techniques in various testing scenarios. Key tools mentioned include Mockito for mocking and WireMock for stubbing.

Uploaded by

backupreneemoura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Mocking vs Stubbing in API Testing + Interview Mock

Curated by Lamhot Siagian

Here’s a comparison table that highlights the key differences between Mocking
and Stubbing in API testing:
Feature Mocking Stubbing
Definition Simulates an API’s Provides predefined
behavior by creating a responses to API
mock object that can requests without
have expectations set enforcing strict
for its usage. behavioral checks.
Use Case Used to verify Used to simulate
interactions between specific responses
components (e.g., from APIs, especially
ensuring specific for external or
methods are called). unavailable services.
Verification Verifies how the mock Generally does not
was used (e.g., verify usage; focuses
checking method calls on returning expected
and arguments). data or behavior.
Behavior Control Allows fine-grained Offers a simpler setup
control over how the with predefined
mock behaves in responses based on
response to various input; no behavior
interactions. checks.
Dependency Useful for mimicking Best for simulating
Simulation complex, dynamic fixed or basic
behavior of a responses from a
dependency. dependency.
State Can be stateful, Generally stateless;
adjusting responses returns a fixed
based on previous response for a given
interactions or states. request.
Implementation Often more complex Simpler and faster to
Complexity due to the need for implement, focusing
configuring only on specific
expectations, request-response pairs.
verifications, and
responses.
Suitability for Tests Ideal for integration Primarily used for
tests where isolated unit tests or
interactions between scenarios where
adjusting responses returns a fixed
based on previous response for a given
interactions or states. request.
Implementation Often more complex Simpler and faster to
Complexity due to the need for implement, focusing
configuring only on specific
expectations, request-response pairs.
verifications, and
responses.
Suitability for Tests Ideal for integration Primarily used for
tests where isolated unit tests or
interactions between scenarios where
modules are important. dependencies are
unavailable.
Tools for API Testing Examples: Mockito, Examples: WireMock,
Moq, EasyMock. MockServer, Sinon.js.
Real-World Analogy Like a "spy" that tracks Like a "shortcut" that
how an object is used gives a preset answer
and validates behavior. without observing how
it’s used.
Error Simulation Can simulate complex Typically used to
error responses by simulate simple error
adjusting expected responses in a fixed
behavior dynamically. manner.
Common Scenarios Testing complex Testing service
dependencies where availability, returning
method calls or specific HTTP statuses,
sequences are or simulating delays.
important.
Scope of Use More commonly used in Commonly used in both
unit testing where integration and unit
dependencies and their tests for predefined
behavior are validated. response testing.

Here are six case-based interview questions related to Mocking and Stubbing in
API Testing with responses structured in the STAR (Situation, Task, Action,
Result) format.

1. Question: Describe a situation where you needed to test an API


but one of the dependent services was unavailable. How did you
handle it?
Answer:
● Situation: I was testing an API that required interaction with a third-party

payment service, which was down for maintenance.


● Task: To ensure our API was tested comprehensively without waiting for
the external service to come back online.
● Action: I used WireMock to create a mock version of the payment API,
simulating its expected responses for different request scenarios. I
configured WireMock to return various success and error statuses,
allowing us to test our API's handling of these cases.
● Result: I was able to complete the testing within the deadline, and we
identified and fixed an issue related to error handling that could have
impacted our users.

2. Question: How did you use stubbing to ensure consistent test


results when testing an API with unpredictable third-party
responses?
Answer:
● Situation: Our application relied on an external geolocation API, but its
responses varied depending on real-time data, which made testing
inconsistent.
● Task: To create a stable testing environment for our API without being
affected by the third-party's unpredictable responses.
● Action: I set up stubbing in MockServer to return predefined geolocation
data based on specific input values. This allowed me to control the
response content and ensure consistent outcomes in our tests.
● Result: The stability of the test environment improved, allowing our tests
to run reliably in CI/CD pipelines and reducing false positives by 30%.

3. Question: Give an example where you used mocking to handle


error responses from a third-party service in API testing.
Answer:
● Situation: I was testing our API’s error handling with a third-party
authentication service, which would occasionally return 500 errors.
● Task: To simulate various error scenarios, including 500, 401, and
timeout errors, so our API’s resilience could be properly tested.
● Action: Using Mockito, I mocked different error responses from the
authentication service, creating tests that would trigger each specific
error and observe how our API managed them.
● Result: Our API’s error handling was refined, ensuring that it could
gracefully handle all errors without crashing. This prepared us for real-
world scenarios and increased system reliability.

4. Question: Describe a time when you used mocking to test


performance or load under controlled conditions.
Answer:
● Situation: Our API depended on an external database service that could
not handle high load for testing purposes.
● Task: To simulate high-load scenarios without overloading the real
database service.
● Action: I implemented a mock version of the database responses using
WireMock, generating responses quickly without interacting with the real
database. This allowed me to simulate thousands of requests in a short
time.
● Result: We identified several bottlenecks in our API’s performance and
optimized response times by 20%, enhancing our API’s performance
under load.

5. Question: Can you share how mocking/stubbing helped you test


multiple scenarios in a short period?
Answer:
● Situation: Our API was complex, involving multiple dependencies like
authentication, payment, and notification services. Testing each
dependency under different scenarios would have been time-consuming.
● Task: To test multiple scenarios (success, failure, and edge cases)
quickly to meet a tight deadline.
● Action: I used WireMock to stub responses from each dependency,
quickly switching between different scenarios (e.g., success, timeout,
partial failures). I wrote scripts to automate the switching of stubs based
on the test being run.
● Result: Testing time was reduced by 50%, allowing us to cover all cases
in time for release, with increased confidence in our API’s robustness.

6. Question: Have you ever needed to mock an API dependency to


test for specific data conditions? How did you approach it?
Answer:
● Situation: While testing an e-commerce API, I needed specific customer
data scenarios that were difficult to recreate in the production-like test
environment.
● Task: To simulate conditions like invalid payment methods, restricted
items, and unusual discount combinations for testing purposes.
● Action: I used WireMock to set up stubs with tailored responses for each
scenario. For example, I created responses that represented failed
payments, out-of-stock items, and special promotions to mimic the
unique test cases.
● Result: We validated complex edge cases and improved the API’s
handling of unusual conditions, enhancing both stability and user
experience.

You might also like