Mocking APIs

About this task

API mocking simulates an API’s behavior by returning predefined responses to requests. It 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

  1. On the home page, select the project that contains the API you want to mock.
    The Development tab opens with the selected project.
  2. In the Explorer view, select the API file that you want to mock.

    The Editor view displays the selected API file.

  3. Select Form view in the editor.
  4. Select API mocking in the Outline view.
  5. Click Configure mocking and provide the following information:
    • Name.
    • Namespace.
    • Version.
    • Description.
  6. 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.

  7. Click Enable mocking toggle button.
    All paths that are defined in the OpenAPI specification are listed.
  8. Click the path link.
    The methods that are defined for the path in the OpenAPI specification are prepopulated.
  9. To configure responses for individual methods in the path, follow these steps:

    Default responses

    1. Click Add response.
    2. Select the required Response status code.
    3. Click Create a new mock response or reference an existing one.
    4. In the Response header section:
      1. Click Add response header.
      2. Provide the Key and Value details.
      3. Click Create.
    5. In the Response body section:
      1. Click Add mock payload.
      2. Specify the Content-type . For example: application/json
      3. Provide the Mock payload.
      4. 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:

    1. Click Add response.
    2. Define the Condition. For example: ($request.query.status equals 'available').
    3. Select the required Response status code.
    4. Click Create a new mock response. You can also reference an existing one mock response.
    5. In the Response header section:
      1. Click Add response header.
      2. Provide the Key and Value details.
      3. Click Create.
    6. In the Response body section:
      1. Click Add mock payload.
      2. Specify the Content-type . For example: application/json.
      3. Provide the Mock payload.
      4. Click Create.
    Mock response files for default and conditional responses are created.
  10. Click Create.

    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.