Skip to content

API_MockFactory.MockBuilder ​

Class

apex
global class API_MockFactory.MockBuilder

Fluent builder for constructing and registering mock responses.

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.
global API_MockFactory.MockBuilder withTransportFailure()Makes the registered mock simulate a transport failure: the callout throws a CalloutException before any response exists, so the caller sees no status code at all.

body ​

apex
global API_MockFactory.MockBuilder body(String body)

Sets the response body.

Parameters

ParameterTypeDescription
bodyStringThe response body (supports {{request.field}} interpolation)

Returns API_MockFactory.MockBuilder — This builder for chaining

fromResponse ​

apex
global API_MockFactory.MockBuilder fromResponse(HttpResponse response)

Populates the builder from an existing HttpResponse.

Parameters

ParameterTypeDescription
responseHttpResponseThe HttpResponse to extract body, status code, and headers from

Returns API_MockFactory.MockBuilder — This builder for chaining

register ​

apex
global void register()

Registers the mock response in the factory.

status ​

apex
global API_MockFactory.MockBuilder status(String status)

Sets the HTTP status text.

Parameters

ParameterTypeDescription
statusStringThe HTTP status text (e.g., "OK", "Internal Server Error")

Returns API_MockFactory.MockBuilder — This builder for chaining

statusCode ​

apex
global API_MockFactory.MockBuilder statusCode(Integer statusCode)

Sets the HTTP status code.

Parameters

ParameterTypeDescription
statusCodeIntegerThe HTTP status code

Returns API_MockFactory.MockBuilder — This builder for chaining

withFailureRate ​

apex
global API_MockFactory.MockBuilder withFailureRate(Integer failureRate)

Sets the failure rate percentage for fault injection.

Parameters

ParameterTypeDescription
failureRateIntegerPercentage of requests to fail (0-100)

Returns API_MockFactory.MockBuilder — This builder for chaining

withHeader ​

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

Adds a single response header.

Parameters

ParameterTypeDescription
nameStringThe header name
valueStringThe header value

Returns API_MockFactory.MockBuilder — This builder for chaining

withHeaders ​

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

Adds multiple response headers.

Parameters

ParameterTypeDescription
headersMapMap of header key-value pairs

Returns API_MockFactory.MockBuilder — This builder for chaining

withTransportFailure ​

apex
global API_MockFactory.MockBuilder withTransportFailure()

Makes the registered mock simulate a transport failure: the callout throws a CalloutException before any response exists, so the caller sees no status code at all. Use it to prove code that must treat a statusless failure differently from a failed status — for example, chain-level retryOn keeps statusless failures retryable. Body, status code, and headers on the same registration are ignored: a transport failure never produces a response.

Returns API_MockFactory.MockBuilder — This builder for chaining.

Example

apex
API_MockFactory.forService('API_SendEmail').withTransportFailure().register();