using mock servers throughout the api lifecycle

APIGit

2023-04-25

The-Many-Ways-to-Use-Mock-Servers

Using Mock Servers throughout the API Lifecycle

Mock servers are a helpful solution for the various unexpected challenges that arise throughout the development process. Integrating with multiple APIs can be time-consuming, with individuals spending an average of 16% of their time debugging, compared to just 10% in their ideal state. External dependencies and concurrent team efforts can also cause delays and frustration. However, by using mock servers, developers can minimize these setbacks and interruptions, making them a valuable tool throughout the entire development lifecycle.

What exactly is a mock server?

To clarify, a mock server is a tool available in the Apigit Platform that enables developers to simulate API requests and responses. When a request is sent to the mock server, it matches the request configuration to saved examples and provides a response with the corresponding data.

Mock servers provide a way to simulate the responses of an API in a production environment, without requiring the developers to write any code. By using mock servers, the feedback loop in API infrastructure development is shortened, enabling faster iterations and more rapid development.

In this blog post, we will discuss the benefits of using mock servers and how they can improve your development workflow. Apigit provides several methods for creating a mock server, which we will explore in detail.

Using mock servers for collaboration

In most cases, development projects involve multiple teams and individuals working on different aspects of the project simultaneously. This includes frontend developers, backend developers, QA engineers, and other specialists, even in smaller-scale projects. In order to maximize productivity and efficiency, it's common for teams to work on different parts of the project concurrently.

Mock servers are a versatile tool that can help maintain efficiency throughout the development process, benefiting both API producers and consumers. They can be utilized at various stages of the API development lifecycle. For example, when the frontend team wants to integrate data from the backend or server, mock servers can be used to simulate API endpoints that are not yet fully configured or functional. This ensures that frontend engineering is accurate and possible even in the absence of complete API functionality.

Mock servers can be utilized at an early stage of the development process, such as during the design phase. Instead of waiting for the API endpoints to be developed and deployed, Apigit enables developers to create a mock server from an API specification, which can simulate server responses for use in the frontend. This allows UI developers to get started right away by querying the mock server and working with the expected responses. Once the actual API is production-ready, developers can swap out the URL for the mock API responses with the URL for the real API (along with any necessary authorization).

Test efficiently by mocking external dependencies

APIs and development projects are not isolated entities, and backend development often requires integration with external services. However, calls to external servers are beyond your control, and they can produce unpredictable and unreliable results, such as returning 503 Service Unavailable errors. As previously mentioned, debugging API issues can consume up to 16% of overall development time. When a project relies on several external APIs, it's possible to encounter issues accessing APIs experiencing failure, which can hinder progress. However, these issues do not have to bring development to a complete halt.

External API calls are often critical components of development workflows, but relying on external services during development can be risky. To mitigate this risk, mock servers can be used to simulate the experience of accessing an external server and returning expected responses. This is particularly useful in situations where a call to an external server or API is a crucial component of the development process.

In this scenario, mock servers aid in the local testing and development phase of the development lifecycle. Let’s explore a theoretical example:

Suppose you are developing an e-commerce application with an ordering flow, where your API is coupled with a shipping service that needs to stay in sync. Specifically, when an item is added to the cart in the e-commerce app, it should be added to the cart for the shipping service as well, so that the shipping service can keep track of what needs to be shipped. This means that every request made to your API will also trigger a request to the shipping service API.

A visual example of testing an e-commerce site but productivity is stopped because of an issue in the Shipping API A visual example of testing an e-commerce site but productivity is stopped because of an issue in the Shipping API

One crucial part of API development is to ensure that external services or APIs that the project relies on are functional and return expected results. However, calls to external servers are not always reliable and can produce unexpected errors, leading to a need for debugging. In such cases, mock servers come in handy by simulating the experience of hitting an external server and returning an expected response.

During the local testing and development phase of the development lifecycle, mock servers help in isolating individual APIs in the workflow. By configuring a mock server for an external API, such as a shipping service API in the case of an e-commerce application, one can continue working based on expected results from the API despite it being down. This makes the testing process more efficient and reduces the chances of the project getting halted due to API failures.

When testing an e-commerce application's ordering flow, the API needs to stay in sync with a shipping service. However, if the shipping API returns errors or is down, it can cause development and testing delays. To avoid such dependencies, mock servers can be used to simulate expected responses from external services. With Apigit, developers can test each API individually and identify specific issues. By configuring a mock server for the shipping API, developers can continue working based on expected results even if the shipping API is down. The following image shows the same workflow with a mock server replacing the shipping API, allowing testing to continue uninterrupted as long as internal APIs function as expected.

A visual example of testing the e-commerce site while using a mock server to simulate the Shipping API A visual example of testing the e-commerce site while using a mock server to simulate the Shipping API

Talk data to me

Data sharing is at the heart of APIs, regardless of the protocol or HTTP verb used. However, this data can contain sensitive personal information that you may not want to use for testing purposes. On the other hand, using placeholder data like "John Doe" can get monotonous and unrealistic. That's where mock servers come in handy.

Mock servers are useful for managing both everyday testing and tricky use cases, including those where private data needs to be protected or when you want to simulate a production-like workflow.

With Apigit, you can create dynamic data for your mock servers by generating them from a series of tightly-coupled requests and corresponding response examples. Instead of relying on the default saved example, you can customize the response to suit your needs.

One way to achieve this is by editing the response example to include dynamic variables. This approach enables your mock server to take advantage of the built-in library and provide dummy data that mimics real-world data.

The internet is an unpredictable place—let mocks bring order to your development lifecycle

Developers can adopt a similar mindset to that of sports where they focus on controlling the controllables. In development, the stability of an external API is not always in your control, but mock servers can be used to simulate what should happen, reducing unexpected issues. By using mock servers, developers can create the stability they need to do their work effectively. So, let mock servers be the stability that developers need to accomplish their goals without any confusion or unexpected hiccups.