API Simulation

API Simulation is a technique in distributed software development and quality engineering that emulates the behavior of either particular endpoints or the API as a whole.

Reason for Topic

Modern apps and distributed systems are quite complex, with many back-end services and components involved in ensuring a great user experience. Often many are external 3rd parties or legacy systems and platforms, huge data sets and infrastructure stacks, and tightly monitored and controlled SLAs.  

Development teams that need to build and test apps that use APIs which either don’t exist yet or with new behaviors that haven’t been released are stuck…unless they have a way to simulate the functions of the API in a fast and lightweight manner. 

Introduction / Definition

API Simulation is a technique in distributed software development and quality engineering that emulates the behavior of either particular endpoints or the API as a whole. API Simulation is a fundamental part of testing complex applications without requiring a full operational environment and is often done to speed development time of apps that consume the API, since developers don’t have to wait for it to be created and deployed completely. The simulated API can be designed to return pre-defined data sets or to simulate error conditions to ensure that the code being tested can handle these scenarios.

Benefits & Examples

The benefits of API simulation include faster development and testing cycles, reduced costs associated with testing against live APIs, and the ability to test against various scenarios that may be difficult or impossible to replicate with the real API. Additionally, API simulation allows developers to test their code even before the actual API is available, enabling them to catch and fix issues early on in the development process.  

A key dynamic that API Simulation brings to development teams is control. It is critical to test apps and consuming services when an underlying service’s performance is degraded, or when it provides inconsistent or newly formatted results, or simply when the API is updated to provide a more detailed range of status and error codes. But no one wants to write and deploy APIs that purposely misbehave, so it’s rare that developers bother to prove how their app gracefully handles an underlying ‘bad actor’ API. If you can’t cause a critical API dependency to follow these non-happy paths, how can you say you know how users will experience your app when these things happen?  

This is where API Simulation provides a critical control point for development teams in that they are able to mock/fake common bad data conditions and service failure scenarios to see how their apps will handle such eventualities. 

How ‘API Simulation’ Is Different than ‘Service Virtualization’?

 

API simulation and service virtualization are two concepts that are often used interchangeably but have different meanings. While both of them are used in software development and testing, they have different purposes and functions. In this response, we will explore API simulation, its benefits, and how it differs from service virtualization. 

Service virtualization, on the other hand, is a technique used to create a virtualized environment that mimics the behavior of a specific service or application. Service virtualization is used to test software components that depend on other components that are not yet available or are not ready for testing. By creating a virtualized environment, developers can test their code against the virtualized environment instead of waiting for the actual component to be available. 

Service virtualization is often used in scenarios where dependencies are complex and involve multiple components. For example, testing a payment processing system may require testing against various payment gateways, each with its own set of dependencies. Service virtualization allows developers to test against each of these gateways without having to wait for the actual gateways to be available. 

Another difference between API simulation and service virtualization is the level of detail that is provided. API simulation is typically used to simulate specific scenarios and test the behavior of the code under those scenarios. Service virtualization, on the other hand, creates a more complete virtualized environment that includes all of the components and dependencies needed to test the software component. 

Drawbacks / Gotchas

There are, however, a few drawbacks to API Simulation, namely: 

  • Incomplete or partial representation of the underlying service: while it may be fast and easy to spin up a simulated API, simulations are often just what was needed at a prior time and lacks new endpoints related to the app being developed or tested. This leads to a kind of additive whack-a-mole where only the endpoints critical to what’s being worked on ever get created.

    The alternative is to automatically generate all of the endpoints and default data from an API specification, though often more advanced responses and data scenarios are not included. Customizations often get lost when regenerating artifacts and this varies from one tool or platform for simulation to another.
     

  • Out-of-sync with actual or future version of underlying service: as mentioned above, it is important to both have control over API simulations while also reusing API specifications to keep the simulation in sync with reality. Specification formats like OpenAPI / Swagger, when properly and thoroughly authored, combined with technology that supports them for API Simulation, provide a ‘source of truth’ about the API’s schema.
     
  • Incomplete data: because a Simulation is not the ‘real implementation’ of the API, it is often not connected to a real data store and therefore only includes the data you design it to respond with. As such, many mocks and lightweight service virtualization techniques fail to cover a variety of data-specific bugs, failure scenarios, and subsequent code branches in the app. Therefore, it is important to understand and balance what data is used in API simulations vs. what comes ‘out of the box’. Some tools and platforms even provide capabilities to automatically generate default data that matches usage and testing scenarios so that havingt to source the right data in early development cycles doesn’t itself become a bottleneck or delay.
     

Summary

API Simulation is a modern, powerful technique in quality engineering aimed to speed coding and testing cycles early in software development and delivery. It provides teams with critical control over building apps that don’t just do the right thing on good days, but also handle bad behaviors from downstream API dependencies often encountered in complex, distributed systems and cloud environments. Though it only goes so far and requires mindful implementation, it can help teams ship changes faster with fewer defects, ultimately saving organizations time and money while ensuring an excellent user experience.