Developer experience (DevEx) is crucial to the success of every API. Developers look for APIs they can understand easily and seamlessly implement. Ensuring excellent DevEx requires you to rigorously test your APIs to ensure they perform as expected. In addition to testing, API mocking can also help warrant developers to have a good experience with your APIs. API mocking is a complementary practice to API testing (though commonly misrepresented as the same thing), and it’s part of an overall approach that prioritizes DevEx as a crucial part of building a high-quality API product.
This article explains the purposes and differences of API mocking and API testing, as well as describes how incorporating mocking with your testing practices can help you create an excellent API experience for developers.
What Is API Mocking?
API mocking creates a copy, imitation, or simulation of a real API, which in turn, will allow you to obtain realistic responses. The mock API behaves the same as the real API, but it lacks some of its characteristics. Mocking is a useful strategy when it’s impractical to use the real API, such as when testing a complicated use case or when using the real API would be costly in terms of dollars or performance
You design the mock API to have the basic functionality you require for your specific use case. For example, you might use a mock API in the following cases:
- During the development phase of an application or integration. You use mocking to replicate dependencies you may not have access to, or that still need to be developed.
- For functional testing. You create a mock to understand how well a certain API component, such as security, works.
- For non-functional testing. Mocking helps ensure that the API behaves correctly when it comes to SLA and performance requirements.
- To provide a cost-effective way to test your API. Potential consumers can try the mock API without incurring the costs of the real API.
- To validate functions in your API that depend on third-party APIs. You can mock third-party API functionality to avoid hitting rate limits and getting charged additional fees.
Mocking helps you understand your customers’ use cases in detail earlier in your API creation process by making it easy to collect feedback on the design and by letting users build small-scale integrations before you create the whole API. You can simulate specific scenarios with your mocks, ensuring the API design works well for different use cases long before you release the API for production.
API mocking involves creating dummy data the API will return in its responses. See this guide for details on creating mock API response data using a dummy server with manual data, a mock data generator for OpenAPI, or a mock API REST server.
What Is API Testing?
API testing, in contrast to mocking, is done to ensure your API is working well. It generally involves making requests to a single API endpoint or multiple endpoints and then validating the responses to confirm that the API behaves as expected. You can analyze the API’s responses to gain insights into its performance, reliability, security, and functionality. You should perform a variety of tests on your APIs, including the following:
- Validation tests. Validation tests determine the effectiveness of your API, whether your API behaves correctly, and if you’ve built a good API product.
- Performance tests. Performance tests assess how your API performs under different scenarios and expose instabilities. Performance tests you could run include load, soak, stress, spike, and peak.
- Security tests. Security tests help you discover if your API is vulnerable to external attacks and threats. Two types of API security tests are penetration and fuzz.
- Contract tests. If you use an API description language such as OpenAPI (and you should!), you can run contract tests to ensure your API continues to follow the language structure.
API mocking should be part of a comprehensive testing strategy, keeping in mind that it can only replace some aspects of real-world testing. You need to continuously test your APIs to ensure that you always provide developers with a high-quality, reliable, and secure product.
Mocking + Testing = A Better Developer Experience
Good DevEx for an API involves numerous things, but incorporating mocking AND testing into your API development process is an important aspect. With these combined practices, you can answer questions that directly impact how developers use and respond to your API products:
- Functionality: Is the API helping developers complete the task at hand? How well have you implemented your API?
- Usability: Are developers finding the API easy to use? How are they using the API?
- Understandability: Do developers understand what the API does?
- Simplicity: Is the API simple to use? How long is it taking developers to get to Hello World?
- Reliability: Does the API meet SLA requirements? Are developers seeing high latency or errors?
Mocking and testing your APIs helps you provide developers with good API experiences by validating the quality of your API designs and ensuring your APIs perform well once deployed.
Incorporate Mocking into Your Testing Practices
You have numerous options for testing your APIs and applications, and many tests can effectively use mocking. However, you generally wouldn’t use mocking for unit testing or end-to-end testing. And if you want to see how your API works under real-world conditions, you need to call the actual API and its dependencies. Testing focused on ensuring the live production API functions as desired should not be done with mock servers.
The broader the type of test, however, the more appropriate mocking is as part of your practice. So, when should you make use of mocks in your testing practices? You should consider using API mocks for all of the following:
- Functional testing. You can let users test parts of the API design without needing a fully running backend or before all the API components are available.
- Negative testing. You can test the API using mocks to see how it will handle problems like invalid messages, overly long response times, and unexpected errors.
- Getting user feedback faster. Mocking APIs lets users beta test different components and provide helpful feedback early in the design process. Customers can evaluate your APIs early in the process.
- Reducing external dependencies during testing. You may want to mock third-party APIs your application or API relies on so that your tests have fewer external dependencies. You will need to update the mocks if any external third-party APIs change.
- Testing advanced scenarios and use cases. Your customers may have edge cases that require a complicated setup. Mock APIs can help you speed up the process of testing complicated user scenarios by returning fake but realistic data. You can test the API for edge cases before writing a lot of code.
Ideally, a robust mocking practice will reduce the burden on your later testing efforts because you only test code that you know serves its purpose for users. You won’t waste time fully testing components only to disable them later.
What Do You Need to Start API Mocking?
Initially, mocking an API begins with creating a mock server. You also need to create fake data that the mock API returns in its responses. You can find many tools for creating mock APIs. Each one differs in how it establishes the mock server and API response data; approaches include the following:
- Mock the server from the code. Some libraries will create the mock server by using your application source code. The library includes a component that mocks the responses as it intercepts real calls made to the application server.
- Local offline mock servers. Some tools allow you to create a local offline mock server. You may need this ability if you run tests that require low latency or need to test advanced scenarios and use cases.
- Remote web-based mock servers. These tools provide an easy-to-use interface to create an online mock server. A few offer an advanced UI to design a mock server with more features. These tools allow you to generate custom HTTP responses quickly.
- API design platforms (like Stoplight!). Some API design platforms include tools for creating mock servers automatically from API description language documents. For example, you can use Stoplight Studio or Prism to create realistic mock servers from OpenAPI documents. Prism is Stoplight’s open-source HTTP mock and proxy server, and Stoplight Studio is our OpenAPI visual editor.
All of these tools can help you create effective mock servers and APIs. The type of mocking tool you’ll need depends on your specific use case and the reasons for mocking APIs.
Ensuring Quality Throughout the API Lifecycle
API mocking and API testing are best practices for building high-quality APIs, leading to lasting engagement with your API users. And just like your API development, mocking and testing are ongoing. Keep revisiting these practices as your product evolves and your users’ needs change, and you’ll find that they help your iterations be more purposeful and productive throughout your product lifecycle.
Stoplight’s design-first approach to APIs emphasizes high-quality development practices, including mocking and testing. Stoplight Platform and Prism are both tools that put API mocking within reach for anyone just getting started. We can help you delight your API users and keep them happy for the long haul.