APIs are most commonly and widely used in modern applications. To build the complete application frontend, backend, and databases are required, and to make proper communication, the APIs are required. But what happens when the backend isn’t ready or third-party APIs are unstable?. This is the point at which the use of API mocking comes in. By using the API mocking, developers can build, test, and debug their applications or products faster without waiting for real APIs to be live.
In this guide, we will explore some important points of API mocking and will be clearing up some questions like what API mocking is, how it works, what the benefits of API mocking are, what its types are, what the different tools are, and how Keploy helps you mock APIs seamlessly.
What is API Mocking?
API mocking is the act of simulating the functionality of a live API by implementing a fake or mock, testable version of it without using the real backend APIs. Rather than sending a request to a live backend or a third-party service, your app talks to these "mock or fake" APIs. The mock API responds with predefined responses, enabling you to test your application's logic and user interface in a predictable and isolated setting.
In software development, "mock" is used to describe a test API that behaves like an actual API. A mock API is different from a real API in that it has the purpose of being used for testing. A mock API keeps tabs on how it is being used, so tests can check whether the right methods had been called with the appropriate parameters.
Example : Building a Weather App
Imagine you are a frontend developer and developed the UI/UX for the weather application. You have added the components to show the current temperature, humidity, and weather icon. And currently the backend developer team is working on modeling and building the real weather APIs, but you need the APIs to test if the components display the correct information.
So by using the API mocking, you design the mock weather API and use it for testing.
Let’s create the mock API step by step.
1. Create a mock response:
json
{
"temperature": 25,
"humidity": 60
}
Point your application to the fake: You are just telling your application to get the weather data from this fake file on your computer instead of using the real API from the internet.
Start working immediately: Now your application can get this fake data and display it on the UI.
So by using the API mocking, we can test our application in a predictable environment without using the real APIs.
What is API stubbing?
API stubbing is just like API mocking, but it is a very basic fake version of an API. A stub gives back a fixed and hardcoded response whenever your app calls it. It does not track what is happening; it just provides the data that is required and keeps running the application without errors.
API stubs are mainly used to check the end state, without knowing how the action was done.
Key Differences Between Mocking and Stubbing:
- Purpose:
* **Stub**: Provides a hardcoded response so that the app runs continuously.
* **Mock:** Simulates the real API’s behavior and can return different responses.
- Behavior:
* Stub: Very basic, returns predefined data.
* Mock: Smarter, as it can mimic different scenarios, errors, and behaviors of the real API.
- Tracking:
* Stub: It is good for ***state verification.***
* Mock: It is good for ***interaction verification.***
- Example:
* **Stub: Call →** */weather*
**Returns →** *{ "temperature": 25 }*
* **Mock: Call** → /*weather?city=Mumbai*
**Return** → *{ "temperature": 30 }*
When Do We Need Mocking API?
The real API is not yet available.
The API is slow or unreliable.
Using a third-party service is expensive.
You need to simulate error states.
You need to test non-idempotent operations.
Benefits of API Mocking:
Accelerate Development Process: It helps the frontend and backend developers to work in parallel, without waiting for the other to finish.
Cost Efficiency: It helps in reducing the cost by preventing us from paying for the third-party APIs.
Improved Quality and Reduced Errors: API mocking helps in error handling and in different scenarios that are hard to replicate in a live environment.
Enhanced Collaboration: Helps in providing a stable and consistent environment for all teams.
Types of Mock APIs:
- Static Mocks:
* Static mocks are the most common and basic type of API mocking. They always return the predefined response for a specific request, no matter what the condition is.
This helps in quickly building and testing a user interface when you don’t need a lot of complexity.
* ***Example***: Consider you are building a “get user details” feature. A static mock would return static data for the user.
- Dynamic Mocks:
* Dynamic mocks are much smarter than static mocks. They have the ability to change their response based on the requested input. This is perfect for testing how exactly your application handles different situations and user inputs.
* *Example:* *Consider you are building a “get user details” feature. A dynamic mock would return dynamic data for the user, based on userId.*
- Contract-Based Mocks:
* Contract-based mocks are like a common blueprint. They produce responses based on a formal API specification, such as an OpenAPI document. That way, the front-end and back-end teams are both coding off the same set of rules, which catches misalignments and mistakes early in the development process. It is similar to having a comprehensive architectural plan that is being adhered to.
* *Example: The contract-based mock APIs specify and say the user’s name must be a string and their age must be a number. It would immediately return an error if you tried to mock a response where the age was a string.*
- Behavior-Driven Mocks:
* Behavior-driven mocks are the most advanced version. They are used to check how a real API would *behave* under pressure. This includes simulating slow responses, network errors, server failures, or a [rate limit](https://keploy.io/blog/community/create-api-rate-limiting-with-token-bucket). They are essential for testing complex API endpoints.
* *Example: You must test your retry logic in your app. A behavior-driven mock can be set up to deliberately respond with a "500 Internal Server Error" on the first two requests and then a "200 OK" on the third. This allows you to verify your app successfully attempts a retry after a failure.*
How to Mock an API Call:
API mocking helps in creating the fake version of real APIs so you can test your application without waiting for your actual backend APIs. Here’s how you can do it step by step:
- Find the API you need to mock:
* Find out on which real APIs your application is dependent. For example, if your app needs a *“/getUser”* endpoint, then we will mock this required API.
- Write down the rules (contract):
* Define some rules before api mocking like how should api looks like, the type of request it expects (GET, POST, etc.), and what kind of data it should return.
- Set up the mock server:
* Next, use an API mocking tools like postman to run a fake server on your computer.
-
Add sample response:
*
json { "id": 101, "name": "John Doe", "email": "john@example.com" } ``` Now every time you call `/getUser` , it will return this data.
- Connect your app to the mock server:
* Now instead of pointing your application to original backend, update the URL to use the mock server.
- Run and test:
* Now when your app makes API calls, it talks to the mock server. You can check if your app displays data correctly, handles errors, and behaves as expected.
What is Mock Data?
Mock data is a fake or placeholder dataset used for testing and development. It's not sensitive and is entirely in the control of the developer. Mock data can either be straightforward, such as a hardcoded JSON response, or as intricate as randomly generated names, addresses, and email addresses that fit into a certain schema.
Mock data is essential in order to test multiple scenarios without endangering security or depending on live, potentially volatile data.
What’s the Difference Between Mocking Internal and External APIs?
The fundamental principal of api mocking are same for both internal and external APIs, but the purpose and benefits are a little different.
- External APIs:
* The external APIs are the third-part service that you do not own.
* **Example**: Social media APIs such as Twitter/X API, or cloud services such as Google Maps API.
* **Why to mock**: Saves money, Avoid limits, Simulate problems.
* Consider the real life example, If you’re creating an online shop. You must test the checkout with stripe. Rather than charging genuine credit cards (expensive and cannot be easily reversed), you simulate Stripe's API so that it will always respond with a "payment successful" throughout testing.
- Internal APIs:
* The internal APIs are used within your organization, usually between various microservices or applications.
* **Example:** Your organization may have one service for users, another for orders, and another for inventory.
* **Why to mock:** Parallel development, Isolation, Safer changes.
* Consider the real life example, If you're developing a food delivery app. There's an Order Service and a Delivery Service. If the Delivery Service isn't implemented yet, you can mock it to make it return simulated delivery updates (e.g., "Your order is on the way"), so the Order Service team can still write tests for their side of the app.
How is API Mocking Different to Stubbing, Simulation and Virtualization?
This can feel confusing because the terms are related, but here’s the difference in plain English:
- Stubbing:
* Stubbing is the most straightforward approach. You just return hardcoded results(e.g: always return “200 OK“). It’s is great to check whether your code works correctly with known inputs/outputs.
- Mocking:
* Mocking is one step forward than stubbing. Mocks can enforce behavior and return dynamic results rather than hardcoded response. They are more flexible and easy to use.
- Simulation:
* A more general term. A simulator attempts to replicate the way an actual system functions. This can involve both stubbing and mocking, but goes usually further, seeking greater precision. Simulations aren't only for testing—they can also be applied to training, demos, or experimentation.
- Service Virtualization:
* The ultimate solution. It does not only simulate one API, it can simulate entire systems, such as databases, mainframes, or groups of APIs in concert. This is utilized predominantly within large enterprise environments to test intricate integrations at scale.
Top Open-Source API Mocking Tools:
- Keploy:
* Keploy is a new open-source mocking tool that goes beyond the scope of conventional [mocking.](https://keploy.io/docs/running-keploy/custom-mocks/) In contrast to tools that mock HTTP traffic alone, Keploy automatically records and mocks all dependencies, such as calls to databases, message queues, and other services. This "record-and-replay" model obviates the drudgery of mock creation from scratch.
- MockServer:
* MockServer cross-platform, generic tool for mocking HTTP and HTTPS APIs, commonly applied in integration tests. Like most others, it concentrates on the API layer and not other dependencies.
- Mockoon:
* A free, open-source desktop tool that enables you to create and execute mock APIs locally with an easy-to-use UI. Perfect for rapid API-level mocks but with data setup done manually.
- WireMock:
* WireMock is an esteemed tool for stubbing and mocking HTTP services. It is Java-based and has a rich suite of features but typically has mocking functionality limited to HTTP traffic.
- Postman:
* Although recognized as an API client, Postman boasts a powerful "Mock Servers" feature. It's convenient and simple to host mock APIs right from your collections, but it mocks the HTTP layer only and cannot simulate databases or other non-HTTP services.
How Keploy Helps You in API Mocking:
Keploy is an open-source and free tool that completely transforms the testing process by automatically creating tests and mocks for you. Instead of manually adding mocks, Keploy generates them automatically by tracking the actions within your application, saving you a significant amount of time.
Keploy vs Other Tools:
There are numerous tools available online, such as Postman and Mockoon, which are great for mocking HTTP traffic. However, these tools have limitations as they cannot mock other crucial aspects of your application, such as databases or external services.application such as databases or external services.
But Keploy solves this by providing a more comprehensive solution for mocking. It does not only mock API calls but even captures and mocks any dependency. This is the primary strength of its capability.
Generates Mocks for Everything: Keploy observes real-world interactions with your API and automatically creates realistic mocks from them. It manages not only API calls but also database queries, message queues like Kafka, and interactions with other internal or external services. Keploy generates a mock for every possible test case, eliminating the need for manual mock creation.
Isolation in True Testing: With Keploy, you can test a service without relying on any of its external dependencies, whether online or offline. It creates a fully isolated and stable test environment using the generated mocks.
Zero Manual Mocks Required: It generates stand-alone tests that do not require manual mock setup. It, in effect, leverages the recorded network traffic to generate the mocks so they will always be current and accurate.
Easy to Use: You can record tests along with their associated mocks by using the Keploy Chrome extension, which records your API calls as you surf.
How Keploy Assists with Various Types of Testing:
-
API Testing:
The Keploy API Test Generator is an AI-powered tool that automatically creates stable, comprehensive API test suites from your API specifications (like OpenAPI/Swagger), Postman collections, curl commands, or even real traffic captured via browser interactions or network recording. It eliminates the need for manual scripting by generating tests that include accurate assertions, cover full API flows (create → update → delete), and detect edge cases and flaky tests.
Key Features includes:
* Generating tests from specs, Postman, curl, or recorded traffic.
* Running tests across different environments (dev, staging, CI) with easy environment switching.
* Self-healing tests that auto-update when your API changes.
* Deduplication to avoid redundant tests.
* Flaky test detection through repeated test runs.
* Collaboration features like grouping, tagging, and sharing reports.
You can use it via the Keploy console, the chrome extension or as part of your CI pipeline, making it easy for developers, SDETs, and QA teams to increase test coverage, reduce test debt, and ensure reliable releases.
-
Unit Testing:
Keploys tools like the PR Agent AI and the VS Code Extension are designed to simplify most frustrating part of Unit testing, it helps in writing the test cases directly. When any changes are made in the code, keploys AI automatically proposes and generates unit tests.
This always keeps track that new code is always covered with test , saving you time and improve the code quality.
Key Features includes:
* Keploy uses the AI to automatically generates unit tests.
* After every updates in code changes it write the relevant and targeted tests.
* It is also integrated with you github workflow via the PR Agent.
* Ensures new code is covered before merging.
* Provides a one-click solution for test generation in VS Code.
-
Keploy has innovative a new approach to integration testing with its eBPF-based technology. It helps in capturing every single network interaction which your application makes, it can be a database calls or any external API calls and uses these recording as mocks.
Key Features includes:
* Helps in recording all network interactions for comprehensive coverage.
Uses eBPF technology for zero-code integration.
It easily mocks all dependencies, including databases and third-party services.
Enables reliable testing without needing external services to be online.
Replays entire user flows as full integration tests.
How API Mocking Operates in the Keploy Process:
Keploy creates the tests and mocks automatically using a unique method called “record and replay”. Testing and recording are the two important steps of process.
1. The Documentation Stage: Documenting Everything:
Keploy uses the eBPF technology when you run your application in record mode. It acts like a network-level transparent observer. It captures each and every logs your application has.
It captures all conversations: Keploy helps in collecting all your app’s activity, we can say it acts like a smart camera. It does not only examine incoming API calls but it also records all the conversations, handles network communications, handles database request, handles external services, and exchanges of data done with other internal services.
The Blueprint is saved: Keploy saves all the data in form of test cases which are easily readable by humans. All the external APIs along with their request and response are saved.
Manages noisy data: Keploy not only stores the api data but it also manages the noisy data which helps to write the correct test cases. This helps to write the correct test cases for your application without storing the irrelevant or inaccurate data.
2. The Test Phase: Replaying with Mocks:
After successfully recording of test cases, you can run your application in Keploy’s test mode. This is how the mocking magic truly happens.
Intercept and Mock: When you application makes a call with external dependency like APIs or database, then keploy interrupt that request.
No Live Services Required: Since Keploy maintains a full record of all dependency responses, you can execute your tests without requiring a live database, a third-party service connection, or some other external dependency. This renders your tests extremely fast and stable.
Automatic Comparison: Keploy eventually compares the new responses of your application to the original captured responses. If there is a variation, it marks them as a failed test cases and it offers a comprehensive report. This helps you to find and improve regressions and other faults**.**
References:
Mastering Mocking: A Complete Guide To Mocks And Other Test Doubles
A Technical Guide To Test Mock Data: Levels, Tools, And Best Practices
Conclusion:
API mocking is a best practice for today's software development. It helps to speed up development, enhance collaboration, and create more stable and robust applications. With the understanding of the fundamental concepts and utilization of powerful open-source tools, developers are able to overcome typical dependencies and optimize their testing process. With tools such as Keploy, the entry point for effective mocking has never been lower, and it is now an available and revolutionary technique for any development team.
FAQs
-
What is the main difference between mocking and stubbing?
The core difference is their goal. Mocks are used for behavioral verification—they check if your code interacted with the service in the right way. Stubs are for state verification—they simply provide the data your code needs to continue running.
-
Is API mocking a replacement for integration testing?
No, it's not. API mocking helps you test individual parts of your application quickly and in isolation. Integration testing is still crucial for making sure your app works correctly with real, live services in a complete environment.
-
Can I mock a database API call?
Yes, absolutely. Modern tools like Keploy can automatically mock calls to a database by recording the queries and responses. This lets you test your data-related logic without needing a live database connection, making your tests much faster and more reliable.
-
What is mock data?
Mock data is simply fake, controlled information used for testing. It has the same structure as real data but uses dummy values. This allows you to test different scenarios safely and predictably, without relying on sensitive or live data.
-
How does Keploy make API mocking easier?
Keploy automates the entire process. Instead of manually creating mock responses, you simply run your application with Keploy. It automatically records all API calls and their responses, then generates mocks and complete test cases for you. This means you get realistic mocks without any manual setup.
Top comments (0)