API_MockFactory
Class · Group: Web Services
global inherited sharing class API_MockFactoryCentral 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:
// 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
| Method | Description |
|---|---|
| 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
| Class | Description |
|---|---|
| MockBuilder | Fluent builder for constructing and registering mock responses. |
| MockResponse | Represents a mock HTTP response with fault simulation options. |
Method Details
clearMocks
global static void clearMocks()Clears all registered memory mocks and invocation history.
Since: 1.0
forService
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:
API_MockFactory.forService('API_SendEmail')
.body('{"messageId":"msg-123"}')
.statusCode(200)
.register();lastRequestContains
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 nameexpectedContent(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
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:
API_MockFactory.registerErrorMock(API_SendEmail.class.getName());registerParseFailMock
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:
API_MockFactory.registerParseFailMock(API_SendEmail.class.getName());wasCalled
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
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