Simulate API responses to test and develop applications without relying on a live
API.
About this task
Note: This procedure applies only to the IBM® webMethods API Gateway.
Mocking replicates endpoints, request-response structures, and error conditions without requiring
an actual backend or fully developed API. Mocking helps developers and testers work with an API
before its implementation is complete. It supports front end development, integration testing, and
automated testing by providing consistent responses. By using API mocking, teams can simulate
different scenarios, test edge cases, and validate application behavior without depending on live
services.
Procedure
- On the home page, select the project that contains the API you want to mock.
- In the Explorer view, select the API file that you want to mock.
- Select Form view in the editor.
- Select API mocking in the Outline
view.
- Click Configure mocking and provide the following
information:
- Name.
- Namespace.
- Version.
- Description.
- Click Save.
The mocking configuration generates three files: the policy sequence file, the route file, and
the endpoint file. The mocking setup references the endpoint file, which is linked to the route
file, and the route file is linked to the policy sequence file.
If a valid policy sequence exists without an associated route, the process attaches the route to
the existing policy sequence instead of creating a new one. If no valid policy sequence is present,
a new policy sequence is created, the route is attached to it, and the endpoint is linked to the
route. These steps are handled internally, requiring no manual intervention.
- Click Enable mocking toggle button.
All paths
that are defined in the OpenAPI specification are listed.
- Click the path link.
The methods that are defined for the path in the
OpenAPI specification are prepopulated.
- To configure responses for individual methods in the path, follow these steps:
Default responses
- Click Add response.
- Select the required Response status code.
- Click Create a new mock response or reference an existing one.
- In the Response header section:
- Click Add response header.
- Provide the Key and Value details.
- Click Create.
- In the Response body section:
- Click Add mock payload.
- Specify the Content-type . For example:
application/json
- Provide the Mock payload.
- Click Create.
Conditional responses
In certain cases, the mock API server returns a response based on specific conditions and
parameters. To add a condition to the operation:
- Click Add response.
- Define the Condition. For example:
($request.query.status equals
'available').
- Select the required Response status code.
- Click Create a new mock response. You can also reference an existing one
mock response.
- In the Response header section:
- Click Add response header.
- Provide the Key and Value details.
- Click Create.
- In the Response body section:
- Click Add mock payload.
- Specify the Content-type . For example:
application/json.
- Provide the Mock payload.
- Click Create.
Mock response files for default and conditional responses are created.
API
mocking creates assets, including policy sequences, routes, and endpoints, and deploys them to the
API Gateway. The gateway
processes these assets and applies the configured mocking rules. The setup removes the need for
manual configuration in the gateway UI, as the assets define the mock responses.
Developers
use the response URL from the API Gateway to send test requests.
The gateway returns mock responses based on the configured status codes, headers, and conditions.
This process helps developers validate API behavior without relying on a fully implemented
backend.