API mocking
API mocking
When to mock
components is mocking, but others are also used, API you create. It imitates the real API, defines mock of those dependencies is created and used.
and partly apply to different things: stubbing, operations that clients of your real API will call, You can make the mock “intelligent” enough to return
receives client requests, and simulates responses. similar results to the actual component it simulates,
mocking, and virtualization (or virtual services).
It establishes a common ground for teams to thus letting development move forward without being
1. A stub: A minimal version of a service or communicate and facilitate artifact sharing. hindered by unavailability of services or systems.
API, usually configured to return predictable
or even hardcoded data. The response is These services are often shared with other As the software development lifecycle progresses,
often coupled to the test case or suite, and development teams and frequently support more the need for more intelligent and dynamic
the tests are going to depend on that data. than one protocol (HTTP, MQ, TCP, etc.). They can responses requires a more intelligent mock. This is
It is used often when the suite of tests is be called remotely (over HTTP, TCP, etc.), whereas why we often see virtual services used for system
simple. Stubs are often handwritten; some stubs and mocks most often work in-process testing where stubs and mocks for unit/module/
can be generated by tools for you. acceptance testing.
directly with classes, methods, and functions.
2. A mock: Usually verifies outputs against some Virtual services are often deployed in an environment
expectations, which can be set in the test. It How are these used in tests? where they can be shared and used by many
is most useful when you have a large suite of teams simultaneously, whereas stubs would be
Developers are usually required to test that
tests and each of the tests needs different individual instances per team.
the service or application they are working on
responses or data, or you want to test different
integrates with other system components via APIs.
scenarios. Maintaining a stub in that case could
be manual and costly, so you can use a mock Frequently, they are unable access those systems
instead. Mocks often focus on interactions and during development due to security, performance,
are usually stateful; for example, you can verify ownership, or maintenance issues that make them
how many times a given method was called. unavailable – or they simply might not exist yet.
Contract testing is a technique used for testing Contract testing is immediately applicable
an integration point by checking each application anywhere where you have two services that need
in isolation. It ensures the messages it sends or to communicate – such as an API client and a web
receives conform to a shared understanding that frontend.
is documented in a “contract.”
Contract testing really shines in an environment
For applications that communicate via HTTP, with many services (as is common for a microservice
these “messages” would be the HTTP request and architecture) are being developed in parallel and
response, and for an application that used queues, need to be tested at speed. Having well-formed
this would be the message that goes in the queue. contract tests makes it easy for developers to avoid
version hell. Contract testing is the killer app for
In practice, a common way to implement contract
microservice development and deployment.
tests (the way Pact does it) is simple. Check that all
the calls to your test-double returns with the same
results that would come from the real application.
Test Environment
documents, but an API is a business asset. And you Why should I care about
API mocking and your teams (and partners) are going to want to
mock “drift”?
demonstrate it and try it out.
In the Agile world of software development, the
Mocks and contract testing are important in validating
the premise of the contract and ensuring that the pace and rate of change of services are fast, and
You can use API definitions and mocks, especially of your development flows and teams are able to
when hosted in tools like SwaggerHub, to automatically accelerate with the right kind of test at the right time.
| The design phase of an API. You can quickly A development company has been developing
collaborate with stakeholders and apply changes microservices. They used OAS-based APIs to
to refine the API contract without waiting for a communicate between them. Teams used to have
server deployment or the development team’s to wait for one another to finish the APIs before
availability. With mocking, you can try out the
starting work. This dependency slowed down
changes and ensure they work as expected.
projects and caused stress.
| Collaboration and reduced dependencies.
This is helpful between development teams, Using API mocks, they could simulate their API API Mocking 102
especially your frontend and backend. It’s perfect schemas and defined behaviors. They developed both
when API is still under development, and you sides of their service in parallel, without having to wait How to create mocks
want to avoid bottlenecks for multiple teams. on code to be written before testing can happen.
and prevent drift
scenarios
via APIs. Unfortunately, it might not always be have a sandbox. We frequently see customers citing
desirable or even possible to access those systems systems owned by production that are not made
during development.
available to testing and cannot be modified to reflect
For example, you might need to test a geo-location | Hitting third-party API rate limits.
This helps you validate the results from your responses it detected. Another approach is to record
component, as they are not affected by eventual a live interaction and replay it back during tests. This
inconsistencies in the external components. So an mock can then be configured to support the test use
added benefit to mocking is that you keep your cases and run as often and wherever required to
usage quota of external APIs to a minimum. support testing.
can configure your service to forward all As you work through the software development
incoming requests to a real API and create process, you can use mocks at different stages to
virtual operations for those requests. test different scenarios and update your mocks to
reflect what you expect real-world conditions to be.
Once you have the recording created and
configured, you can start to build out the other These capabilities provide real value to your teams
parts. The recorded mock can be left to run as and enable development and testing to progress in
often and as long as you need, and your team parallel. They also provide the following benefits:
can utilize it as part of unit or integrations tests.
| Sandbox environments to modify as you need
If you want to validate your mock has not
| Multiple versions to support different test use
drifted, incorporate contract testing against
cases and outcomes
it and monitor changes to the service from
what your producer or consumer expects. | Lower cost of running tests against rated
external services
Then use your API mock in different scenarios.
The Single Source of Truth The All-in-One Automated The Most Comprehensive
for API Development API Testing Platform Contract Testing Platform