preloader
blog-post

How to start with API mocking

author image

API mocking is a technique used to create a simulated version of an API. This can be useful for testing purposes, as it allows developers to test their code without having to rely on the external API.

Using API mocking developers can simulate different scenarios, such as:

  • Happy path: The code handles successful responses from the API.
  • Error scenarios: How the code handles error responses from the API.
  • Performance scenarios: How the code handles performance bottlenecks in the API.
  • Integration scenarios: How the code integrates with other systems that use the API.

API mocking tools

There are a number of different tools available for mocking APIs.

The best tool for mocking an API will depend on your specific needs and requirements:

  • If you are looking for a comprehensive mocking tool with a wide range of features, then WireMock is a good choice.
  • If you are looking for a cloud-based mocking tool that is easy to use, then Mockoon or Mocko are good choices.
  • If you are already using Playwright for your end-to-end tests, then it can also be used to mock APIs.

WireMock

WireMock is a popular open source tool for mocking APIs. It is easy to use and provides a wide range of features, such as the ability to generate mock responses for different HTTP methods and status codes, stub out dependencies, and simulate different scenarios. WireMock also supports recording and replaying real API traffic, which can be useful for debugging and testing purposes.

  • Pros:
    • Easy to use
    • Wide range of features
    • Supports recording and replaying real API traffic
  • Cons:
    • Can be complex to set up for more advanced scenarios

Mockoon

Mockoon is a cloud-based API mocking tool that is easy to use and provides a wide range of features. It allows developers to create mock APIs in minutes, without having to write any code. Mockoon also supports a variety of authentication mechanisms and can be used to simulate different scenarios.

  • Pros:
    • Easy to use
    • Cloud-based
    • Supports a variety of authentication mechanisms
  • Cons:
    • Some features are only available in the paid plan

Playwright

Playwright is a relatively new tool for mocking APIs. It is a popular choice for developers who are already using Playwright for their end-to-end tests. Playwright allows developers to mock APIs by intercepting and handling network requests. This can be done using the page.route() method.

  • Pros:
    • Easy to use for developers who are already using Playwright
    • Can be used to intercept and handle network requests
  • Cons:
    • More limited features than other mocking tools
    • Not as widely used as other mocking tools

Mocko

Mocko is a cloud-based API mocking tool that is easy to use and provides a wide range of features. It allows developers to create mock APIs in minutes, without having to write any code. Mocko also supports a variety of authentication mechanisms and can be used to simulate different scenarios.

  • Pros:
    • Easy to use
    • Cloud-based
    • Supports a variety of authentication mechanisms
    • Free plan available
  • Cons:
    • Some features are only available in the paid plan

Tool comparison

Here is a table summarizing the most important features and disadvantages of the four API mocking tools mentioned above:

ToolFeaturesDisadvantages
WireMockOpen source, wide range of features, supports recording and replaying real API trafficCan be complex to set up for more advanced scenarios
MockoonCloud-based, easy to use, supports a variety of authentication mechanismsSome features are only available in the paid plan
PlaywrightEasy to use for developers who are already using Playwright, can be used to intercept and handle network requestsMore limited features than other mocking tools, not as widely used as other mocking tools
MockoCloud-based, easy to use, supports a variety of authentication mechanisms, free plan availableSome features are only available in the paid plan



API Mocking and E2E

E2E testing is a type of testing that tests the entire system, including the API layer. By mocking the API layer in E2E testing, you are not testing the real API and you may miss potential problems.

In general, API mocking is considered an anti-pattern in E2E testing due to the following reasons:

  • It can lead to false positives. If the mock API does not accurately simulate the real API, then your E2E tests may pass even though there are problems with your code.
  • It can make it difficult to debug problems. If an E2E test fails, it can be difficult to determine whether the problem is with your code or with the mock API.
  • It can give you a false sense of security. If all of your E2E tests pass, you may think that your system is working correctly, even though there are problems with the API layer.

Despite all this, are there any use cases where API mocking is an acceptable technique in E2E testing? Well, they are lots of cases where it is acceptable:

  • An API that is not yet available. When developing a new feature that depends on an API that is not yet available, you may need to mock the API in order to test your feature.
  • An API that is slow or unreliable. If the real API is slow or unreliable, then it may make it difficult to run your E2E tests effectively. Mocking the API in this case may speed up tests and make them more reliable.
  • Testing a specific scenario that is difficult to reproduce with the real API. For example, when testing a scenario that involves a lot of data or that involves multiple API calls. In this case, a mocked API will help to create a controlled test environment.

If after analyzing the current needs of our tests we conclude that we need to mock the API layer, we should observe the following ideas to reduce risks associated with mocking:

  • Only mock the API layer when necessary.
  • Use a mocking framework to simplify the process of creating mock responses.
  • Test the mock API to ensure that it is working as expected.
  • Document the reasons why you are mocking the API layer and the scenarios that you are testing.

Example of mocking an API with Playwright

A basic example of mocking an API with Playwright requires intercepting and handling the network requests of interest.

// General example
// Sets a RESPONSE_BODY mock for the API endpoing and then calls it
await page.route("/api/me", async route => { 
  await route.fulfill({ RESPONSE_BODY })
});

const response = await page.goto("https://www.example.com/api/me");

Should I mock my API for testing?

API mocking is a powerful technique that can be used to improve the quality of your code and reduce the risk of problems in production. However, it is important to use mocking responsibly.

With API mocking the developer creates a simulated version of an API endpoint. This can be useful for testing purposes, as it allows developers to test their code without having to rely on the real API.

In general, mocking an API allows developers to test their code in isolation, to simulate different scenarios, to stub out dependencies, and to test the performance of their code. But for the particular case of E2E testing, API mocking is generally considered an anti-pattern, as it can lead to false positives, make it difficult to debug problems, and give you a false sense of security. However, there are a few caveats where API mocking may be acceptable in E2E testing, such as if the API is not yet available, if the API is slow or unreliable, or if you need to test a specific scenario that is difficult to reproduce with the real API.

When deciding whether or not to mock an API in E2E testing, it is important to weigh the benefits and risks. If there is a good reason to mock the API, such as if the API is not yet available or if the API is slow or unreliable, then the benefits may outweigh the risks. However, if there is no good reason to mock the API, then it is best to err on the side of caution and test the real API whenever possible.

At Kizmet Solutions, we can help you navigate all this process. From our testing expertise we can recommend the API mocking technique that fits your needs.

comments powered by Disqus

Recent Articles

Get a complimentary audit

We offer a free consultation where we will take the time to understand your needs, we are happy to chat with whatever is on your mind

Get in touch
*