Skip to content

API_MockFactory.MockBuilder

Class

apex
global class API_MockFactory.MockBuilder

Fluent builder for constructing and registering mock responses.

Since: 1.0

Example:

apex
API_MockFactory.forService('API_SendEmail')
    .body('{"messageId":"msg-123"}')
    .statusCode(200)
    .register();

Methods

MethodDescription
global API_MockFactory.MockBuilder body(String body)Sets the response body.
global API_MockFactory.MockBuilder fromResponse(HttpResponse response)Populates the builder from an existing HttpResponse.
global void register()Registers the mock response in the factory.
global API_MockFactory.MockBuilder status(String status)Sets the HTTP status text.
global API_MockFactory.MockBuilder statusCode(Integer statusCode)Sets the HTTP status code.
global API_MockFactory.MockBuilder withFailureRate(Integer failureRate)Sets the failure rate percentage for fault injection.
global API_MockFactory.MockBuilder withHeader(String name, String value)Adds a single response header.
global API_MockFactory.MockBuilder withHeaders(Map<String, String> headers)Adds multiple response headers.

Method Details

body

apex
global API_MockFactory.MockBuilder body(String body)

Sets the response body.

Parameters:

  • body (String) - The response body (supports {{request.field}} interpolation)

Returns: API_MockFactory.MockBuilder - This builder for chaining

Since: 1.0

fromResponse

apex
global API_MockFactory.MockBuilder fromResponse(HttpResponse response)

Populates the builder from an existing HttpResponse.

Parameters:

  • response (HttpResponse) - The HttpResponse to extract body, status code, and headers from

Returns: API_MockFactory.MockBuilder - This builder for chaining

Since: 1.0

register

apex
global void register()

Registers the mock response in the factory.

Since: 1.0

status

apex
global API_MockFactory.MockBuilder status(String status)

Sets the HTTP status text.

Parameters:

  • status (String) - The HTTP status text (e.g., "OK", "Internal Server Error")

Returns: API_MockFactory.MockBuilder - This builder for chaining

Since: 1.0

statusCode

apex
global API_MockFactory.MockBuilder statusCode(Integer statusCode)

Sets the HTTP status code.

Parameters:

  • statusCode (Integer) - The HTTP status code

Returns: API_MockFactory.MockBuilder - This builder for chaining

Since: 1.0

withFailureRate

apex
global API_MockFactory.MockBuilder withFailureRate(Integer failureRate)

Sets the failure rate percentage for fault injection.

Parameters:

  • failureRate (Integer) - Percentage of requests to fail (0-100)

Returns: API_MockFactory.MockBuilder - This builder for chaining

Since: 1.0

withHeader

apex
global API_MockFactory.MockBuilder withHeader(String name, String value)

Adds a single response header.

Parameters:

  • name (String) - The header name
  • value (String) - The header value

Returns: API_MockFactory.MockBuilder - This builder for chaining

Since: 1.0

withHeaders

apex
global API_MockFactory.MockBuilder withHeaders(Map<String, String> headers)

Adds multiple response headers.

Parameters:

  • headers (Map) - Map of header key-value pairs

Returns: API_MockFactory.MockBuilder - This builder for chaining

Since: 1.0