Skip to content

API_MockFactory

Class · Group: Web Services

apex
global inherited sharing class API_MockFactory

Central factory for mock response management. Provides test isolation via scoped registries and supports both memory-based (unit tests) and metadata-based (runtime) mocking. Features: Memory mocks for unit tests with test isolation Metadata mocks for runtime/sandbox configuration Call verification for asserting mock invocations Dynamic response interpolation ({{request.field}}) Fault simulation (delays, failure rates)

Since: 1.0

Example:

apex
// Register a custom mock response for an outbound service
API_MockFactory.forService(API_SendEmail.class.getName())
    .body('{"messageId":"msg-123"}').statusCode(200).register();
// Register an error mock
API_MockFactory.registerErrorMock(API_SendEmail.class.getName());

Methods

MethodDescription
global static void clearMocks()Clears all registered memory mocks and invocation history.
global static API_MockFactory.MockBuilder forService(String serviceClassName)Creates a fluent builder for registering a mock response.
global static Boolean lastRequestContains(String serviceClassName, String expectedContent)Verifies that the last invocation contained specific text in the request body.
global static void registerErrorMock(String serviceClassName)Registers an error mock (500 Internal Server Error) for the given service.
global static void registerParseFailMock(String serviceClassName)Registers a parse-fail mock (200 OK with unparseable body) for the given service.
global static Boolean wasCalled(String serviceClassName)Verifies that a mock was called at least once.
global static Boolean wasNeverCalled(String serviceClassName)Verifies that a mock was never called.

Inner Classes

ClassDescription
MockBuilderFluent builder for constructing and registering mock responses.
MockResponseRepresents a mock HTTP response with fault simulation options.

Method Details

clearMocks

apex
global static void clearMocks()

Clears all registered memory mocks and invocation history.

Since: 1.0

forService

apex
global static API_MockFactory.MockBuilder forService(String serviceClassName)

Creates a fluent builder for registering a mock response.

Parameters:

  • serviceClassName (String) - The fully qualified service class name

Returns: API_MockFactory.MockBuilder - A new MockBuilder for chaining

Since: 1.0

Example:

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

lastRequestContains

apex
global static Boolean lastRequestContains(String serviceClassName, String expectedContent)

Verifies that the last invocation contained specific text in the request body.

Parameters:

  • serviceClassName (String) - The service class name
  • expectedContent (String) - Text that should be present in the request body

Returns: Boolean - True if the last request body contains the expected content

Since: 1.0

registerErrorMock

apex
global static void registerErrorMock(String serviceClassName)

Registers an error mock (500 Internal Server Error) for the given service.

Parameters:

  • serviceClassName (String) - The service class name to register an error mock for

Since: 1.0

Example:

apex
API_MockFactory.registerErrorMock(API_SendEmail.class.getName());

registerParseFailMock

apex
global static void registerParseFailMock(String serviceClassName)

Registers a parse-fail mock (200 OK with unparseable body) for the given service.

Parameters:

  • serviceClassName (String) - The service class name to register a parse-fail mock for

Since: 1.0

Example:

apex
API_MockFactory.registerParseFailMock(API_SendEmail.class.getName());

wasCalled

apex
global static Boolean wasCalled(String serviceClassName)

Verifies that a mock was called at least once.

Parameters:

  • serviceClassName (String) - The service class name to verify

Returns: Boolean - True if the mock was invoked at least once

Since: 1.0

wasNeverCalled

apex
global static Boolean wasNeverCalled(String serviceClassName)

Verifies that a mock was never called.

Parameters:

  • serviceClassName (String) - The service class name to verify

Returns: Boolean - True if the mock was never invoked

Since: 1.0