Mock API for Testing - Use Cases, Features, Examples

Dan Perovich
WireMock Head of Sales Engineering & Customer Success
August 2, 2024

While most developers want to adhere to the principle of “test early, test often”, reality tends to be messier. When APIs aren’t ready or available, testing is often incomplete or based on limited dummy data. By using mock APIs, developers can shift testing earlier in the SDLC, and release more reliable and complete applications. This article covers the basics of why, how, and when to use mock APIs for testing.

Why use a mock API for testing?

Implementing mock APIs during development and testing is useful in a variety of situations, such as when you lack dummy data, where internal APIs are unavailable, or if third-party APIs are unreliable. They take the guesswork out of critical validation processes by providing a realistic testing environment that dev teams can rely on to see how their product interacts with other critical APIs before finalizing their code. 

Mock APIs can be integrated into the design and development process to create an isolated and controlled testing environment. Here, API responses and conditions can be managed without having an impact on development or, in cases where a service is already live, the production environment and the end-user. 

Now you might be asking: how is this different from just using dummy values in my code, since it’s not the real API anyway? The answer is that mocks are:

  • Can be more realistic and simulate a wider range of scenarios (if using a mature mocking platform such as WireMock Cloud);
  • Keep your test code close to your production code, so that you literally need to change one endpoint for your code; and
  • Allow you to run full integration tests against the mock API, which can run as a service in your environment, allowing you to test the full application and associated CI/CD pipelines.

Why mock APIs are becoming more popular in dev and Q&A teams

There’s a growing need for mock APIs in all but the very simplest development projects because more and more new applications rely on APIs to function. Pretty much any digital product or service a consumer uses today—be it consumer apps, banking, or physical devices—relies on one or more APIs behind the scenes to function and deliver positive experiences via seamless data transfer.

This can only happen when apps and APIs are designed in a way where flaws and issues can be discovered and rectified early on in the development process. This is especially true when there’s a mix of internal and third-party APIs. Discovering these issues too late can mean they’re expensive or difficult to fix, leading to haphazard workarounds and patches that detract from the end-user experience. 

It’s worth noting that there are many types of API mocking tools - from very simple open source libraries to full fledged virtualization platforms. Throughout this article, we will be referring more to platforms such as WireMock Cloud or open source WireMock rather than simpler implementations built into application code.

Advantages of using mock APIs over dummy data

The biggest advantage of using mock APIs is the ability to simulate various API behaviors such as different response statuses, delayed responses, or throttling to create a more realistic testing environment. They also enable chaos engineering and the testing of complex scenarios and edge cases that might be difficult to reproduce with dummy data alone. Other key advantages include: 

  • Integration with CI/CD: Mock APIs can be integrated into CI/CD pipelines for continuous testing of the app or product with automated test suites. This provides dev teams with immediate feedback on code changes and encourages testing early on in the development cycle. 
  • Minimal effort to move to production: Mock APIs are realistic copies of the internal and/or third-party API(s) that a dev team relies on. This means that moving from a mock environment to production requires minimal effort while providing confidence that the integration with the live API will work as expected.
  • More realistic debugging: Mock APIs provide detailed logs of API interactions, making it easier for dev teams to diagnose and fix issues. Controlled failure scenarios also help teams test a variety of common and uncommon failure states, improving the overall robustness of the application.

Must-have features for API mocking

If you’re going to rely on mock APIs for testing, it’s important that whatever solution you use has a full suite of features to accurately simulate real-world scenarios and get the most value out of your mocks. Here are four features we believe are non-negotiable for achieving this: 

Dynamic responses

The ability to create customized responses based on input parameters enables dev teams to simulate various API behaviors and scenarios and see how their product responds. This ensures that it can handle a wide range of situations. Dev teams can use dynamic responses to create specific conditions that mirror real-world situations, such as different user inputs or system states. 

Implementing conditional logic within mock APIs further enhances this capability by allowing responses to change based on specific conditions, such as query parameters or request payloads. This enables more granular testing and helps to ensure that the product can handle complex and diverse scenarios. 

Chaos engineering

Chaos engineering is a relatively new practice that builds on dynamic responses by enabling dev teams to proactively test system resilience. It works by introducing controlled failure scenarios and measuring their impact. This makes it possible to identify weaknesses and develop strategies for making the system more tolerant of faults. 

It’s critical to include chaos engineering in your mocking workflows because, as any experienced developer will know, unexpected errors and failures are a common occurrence. They regularly do happen, and they’ll happen to your product too. You therefore need to know how your app will respond to various failure scenarios so that you can build in resilience. 

WireMock Cloud includes chaos engineering functionality that enables dev teams to simulate a wide range of API failure scenarios through random chaos elements and deploy better products as a result.

Stateful mocking

APIs often have different states that change as users interact with it. As a result, testing activities in this context require that different responses are served when there’s a series of identical requests. With such requests, however, it can be difficult to differentiate between the first and second (and third and fourth…) requests because they’re all the same.

Stateful mocking is the answer here. It works by maintaining states across multiple requests to simulate real-world interactions that depend on previous states, such as a simple to-do list,  a login session, or a shopping cart. 

WireMock Cloud’s Scenarios feature provides stateful mocking functionality by providing finite state machines that can be used as additional stub matching conditions. They allow more than one definition of an otherwise identical stub, with different responses based on the current state of the machine. 

Ability to run in integration tests

Unit testing helps isolate  components and their logic from external dependencies to ensure that each part of the product works correctly on its own. Mock APIs enable this by supporting the simulation of individual endpoints with predefined responses, thereby allowing issues to be detected at a unit level. 

However, unit testing on its own isn’t enough to satisfy the demands of modern applications. Integration testing is a much better alternative. This is designed to support more complex scenarios where multiple components interact, ensuring that the entire system works together as expected. Integration testing goes beyond the isolated focus of unit tests by simulating end-to-end workflows that encompass authentication, data processing, and API chaining.

Case Study: How OVO Energy Improved Testing with Mock APIs

To understand how mock APIs are used in actual testing use cases, let’s look at a WireMock Cloud customer example.

OVO Energy is a major UK energy and gas supplier in the UK. OVO provides its customers with many different services on its website via third-party integrations. These include information on energy and broadband tariffs and information from local authorities. 

As OVO’s engineering team implemented these integrations via third-party APIs, they frequently ran into reliability issues. Many of them were nowhere near stable enough to run integration tests against because the providers regularly took down their dev environments or provided sandboxes that were poorly maintained. This prevented OVO’s engineering team from running vital process simulations in non-production environments, which they needed to do repeatedly.

To solve these challenges, OVO’s engineering team adopted WireMock Cloud to accurately simulate their process simulations during design, development, and integration testing by validating each API. Because WireMock Cloud’s mock APIs are a near-identical virtual copy of the production APIs, they enabled OVO’s engineers to glean the same results that they would see in production environments even when the live third-party APIs are unavailable. 

OVO’s engineering team now has a stable development and testing environment where they can run daily integration tests against WireMock Cloud’s mock APIs while benefitting from significant efficiencies in their dev workflows. 

Read the full case study

WireMock Cloud for your testing and development

WireMock Cloud includes all the features you need to mock the APIs that you depend on. The platform is built on the open-source WireMock library—the leading enterprise-grade mocking framework—and is offered as a hosted solution not just for response and scenario testing but also API prototyping, mock-driven development, and more.

If you’re interested in learning more, you can try it out and begin mocking instantly by signing up for our free forever account.

/

Latest posts

Have More Questions?