What is API mocking and what are the benefits?

API mocking provides a way to test how an API is likely to perform and integrate within your system. It can help you get stuck into testing rapidly but does have some limitations. Let’s explore what a mock API is and how mocking API calls can help you understand how the real version of your API is likely to perform.

What is API mocking?

API mocking is the creation of a simulated version of an API that mirrors the behaviour of an actual API. It’s essentially creating a fake API for testing. 

While the API mock will lack some of the functionality of the real API, it should correspond to it sufficiently to enable you to carry out the test you need.

The benefits of API mocking

The key benefit of creating a mock API is that you can test it to understand the API’s behaviour. This shows you how the actual API would behave in the same situation, meaning you can:

  • Evaluate API performance – including in complex scenarios 
  • Identify bugs 
  • Iron out any glitches

By testing mocks, you can move ahead quickly with your understanding of how your actual API will perform, before you create your actual API product. This is why using a mock server for API testing has become such a key part of the development process.

Types of mock APIs

Your mock API or mock endpoint should enable you to thoroughly test the performance, integration and functionality that’s important to your use case. Types of mock APIs include: 

  • Public mock/dummy API: a mock API on a public server. If you need to test your API in relation to tooling and services that are hosted elsewhere, this is likely the most practical choice.
  • Local server mock API: a mock API on a local machine. This is ideal if you need offline access or require low latency during your testing.

Static vs dynamic mock data

You will need to decide whether to use static or dynamic mock data when testing your API mock. There are advantages to both. 

With static data, an API mocking tool can generate the dummy data for you, or you can generate them yourself. This is a simple but limited option, as your actual API will behave differently in response to different inputs. As such, static data is great for initial or limited testing, but mocking API responses with dynamic data will give you more thorough insights into your API’s behaviour.

Dynamic mock data allows you to gain realistic insights based on a wide range of different inputs. This gives you a far more comprehensive way of testing mocks’ performance and debugging them.

Best practices for API mocking

API mocking can deliver insights that contribute significantly to the successful development of your actual API. By following these best practices, you can maximise your chances of getting the best out of your mock.

  • Make sure that your API mock mirrors the actual one as closely as possible: use the same schemas, transport protocols, etc. and mock external dependencies. If you don’t, the mock API can’t serve its purpose in terms of comprehensive and ongoing testing.
  • Use your mock for error testing, as well as evaluating what good looks like. Throw everything you can think of at your API mock, from extended response times to invalid messages, then see how it responds. You’ll be looking for graceful error handling in your actual API, so put your mock through its paces in respect of dealing with both complexity and failure.

How to implement API mocking

You can implement API mocking with the following steps:

  • Identify your API endpoints, request methods and expected responses
  • Choose a mocking tool or library
  • Set up your mock server
  • Define the mock responses
  • Start the mock server
  • Configure your application
  • Test your application’s behaviour in various scenarios, including edge cases and errors
  • Update as required to match changes to the actual API

Common scenarios

Scenarios where API mocking really comes into its own include: 

  • Mocking during development: you can provide a project-specific development and testing environment for both operational teams and architects to test.
  • Integration and demonstration purposes: demonstrate to your potential customers how your API would function and integrate. This can be a handy way to showcase your product without deploying the real API.
  • Functional tests: identify bugs, see how an API handles errors and evaluate its ability to deal with complex cases using functional mock API testing.
  • Non-functional testing: evaluate performance, response times and more. With an API mock, you can run through multiple scenarios rapidly, without the need to create a real backend.
  • External components: test whether an API plays nicely with external components. If the external dependencies aren’t yet available for you to do so, mocking those components can provide the ability to test them until the real ones become available.

How is API mocking different to stubbing, simulation and virtualisation? 

This basic functionality is what sets a mock API apart from other forms of simulated APIs used for testing. Stubbing, for example, refers to the use of a placeholder API that lacks any real functionality. An API mock, however, provides the functionality required to make it useful for testing purposes (but doesn’t go as far as simulation and virtualisation, which provide greater degrees of functionality than mocking).

What’s the difference between mocking internal and external APIs?

Mocking an internal API means you are in control of everything from how you create the mock to how you test it. You’ll also need to keep up with changes made during the API’s development, to ensure your API mock reflects those.

Mocking an external API can be more of a challenge. You’ll need to keep your mock API up to date with any changes but won’t necessarily have access to the API definition. Storing live results for use with your API mock can help here.

Which tool to use to mock APIs

We mentioned choosing a mocking tool or library above. 

  • An API gateway (such as Tyk) can provide you with mock response middleware.
  • Handy tools such as JSON Server, Nock and WireMock are useful if you’re mocking an API for the first time.

Ready for more? Then check out our articles on API strategy and API governance for all you need to know.