API_InboundTestHelper
Class · Group: Testing
global inherited sharing class API_InboundTestHelperClass has base methods that can be used to assist with testing inbound service calls
Since: 1.0
Example:
API_UpdateInvoice.DTO_Request request = new API_UpdateInvoice.DTO_Request();
request.invoiceId = invoice.Id;
request.status = 'Paid';
API_InboundTestHelper.setupRestContext(request);
API_Base handler = API_InboundTestHelper.assertCallSuccessful(API_UpdateInvoice.class.getName());Methods
| Method | Description |
|---|---|
| global static API_Base assertCallAborted(String serviceName) | Executes a callout and ensures that the call was aborted due to the API being disabled. |
| global static API_Base assertCallAborted(String serviceName, String exceptionMessage) | Executes a callout and ensures that the call was aborted due to the API being disabled. |
| global static API_Base assertCallAborted(String serviceName, String exceptionMessage) | Executes a callout and ensures that the call was aborted due to the API being disabled. |
| global static API_Base assertCallFailed(String serviceName) | Executes an inbound service call and asserts that it failed. |
| global static API_Base assertCallFailed(String serviceName, DTO_Base dtoRequest) | Executes an inbound service call with a DTO body and asserts that it failed. |
| global static API_Base assertCallSuccessful(String serviceName) | Executes an inbound service call and asserts that it completed successfully. |
| global static API_Base assertCallSuccessful(String serviceName, DTO_Base dtoRequest) | Executes an inbound service call with a DTO body and asserts that it completed successfully. |
| global static API_Base assertCallSuccessful(String serviceName, String, String> headers) | Executes an inbound service call with a DTO body and custom headers, asserts successful completion. |
| global static void setupRestContext() | Sets up the RestContext objects of "request" and "response" to prepare for testing inbound service calls. |
| global static void setupRestContext(DTO_Base dtoRequest) | Sets up the RestContext objects of "request" and "response" for testing purposes. |
| global static void setupRestContext(DTO_Base dtoRequest, Map<String, String> headers) | Sets up the RestContext with a serialized DTO body and custom headers. |
Method Details
assertCallAborted
global static API_Base assertCallAborted(String serviceName)Executes a callout and ensures that the call was aborted due to the API being disabled. This method takes a service name and returns an instance of the handler for further checks.
Parameters:
serviceName(String) - The name of the inbound webservice handler
Returns: API_Base - An instance of the handler, in case further checks are necessary
Since: 1.0
Example:
API_Base result = API_InboundTestHelper.assertCallAborted('API_InboundTestHelper');assertCallAborted
global static API_Base assertCallAborted(String serviceName, DTO_Base dtoRequest, String exceptionMessage)Executes a callout and ensures that the call was aborted due to the API being disabled. This method takes a service name and the request DTO, sets up the RestContext, and verifies that the expected exception message is present in the response.
Parameters:
serviceName(String) - The name of the inbound webservice handlerdtoRequest(DTO_Base) - The request being sent the handlerexceptionMessage(String) - The message expected in the response/errors payload
Returns: API_Base - An instance of the handler, in case further checks are necessary
Since: 1.0
Example:
API_Base result = API_InboundTestHelper.assertCallAborted('API_InboundTestHelper', new DTO_Base(), 'An error occurred');assertCallAborted
global static API_Base assertCallAborted(String serviceName, String exceptionMessage)Executes a callout and ensures that the call was aborted due to the API being disabled. This method takes a service name and checks if the expected exception message is present in the response/errors payload.
Parameters:
serviceName(String) - The name of the inbound webservice handlerexceptionMessage(String) - The message expected in the response/errors payload
Returns: API_Base - An instance of the handler, in case further checks are necessary
Since: 1.0
Example:
API_Base result = API_InboundTestHelper.assertCallAborted('API_InboundTestHelper', 'An error occurred');assertCallFailed
global static API_Base assertCallFailed(String serviceName)Executes an inbound service call and asserts that it failed.
Parameters:
serviceName(String) - The name of the inbound webservice handler
Returns: API_Base - An instance of the handler for further assertions
Since: 1.0
Example:
API_Base handler = API_InboundTestHelper.assertCallFailed('API_Echo');assertCallFailed
global static API_Base assertCallFailed(String serviceName, DTO_Base dtoRequest)Executes an inbound service call with a DTO body and asserts that it failed.
Parameters:
serviceName(String) - The name of the inbound webservice handlerdtoRequest(DTO_Base) - The DTO that should be serialized as the request body
Returns: API_Base - An instance of the handler for further assertions
Since: 1.0
Example:
API_Base handler = API_InboundTestHelper.assertCallFailed('API_Echo', new DTO_Base());assertCallSuccessful
global static API_Base assertCallSuccessful(String serviceName)Executes an inbound service call and asserts that it completed successfully. Sets up RestContext internally before executing. Mirrors the outbound test helper pattern.
Parameters:
serviceName(String) - The name of the inbound webservice handler
Returns: API_Base - An instance of the handler for further assertions
Since: 1.0
Example:
API_Base handler = API_InboundTestHelper.assertCallSuccessful('API_Echo');assertCallSuccessful
global static API_Base assertCallSuccessful(String serviceName, DTO_Base dtoRequest)Executes an inbound service call with a DTO body and asserts that it completed successfully.
Parameters:
serviceName(String) - The name of the inbound webservice handlerdtoRequest(DTO_Base) - The DTO that should be serialized as the request body
Returns: API_Base - An instance of the handler for further assertions
Since: 1.0
Example:
API_Base handler = API_InboundTestHelper.assertCallSuccessful('API_Echo', new DTO_Base());assertCallSuccessful
global static API_Base assertCallSuccessful(String serviceName, DTO_Base dtoRequest, Map<String, String> headers)Executes an inbound service call with a DTO body and custom headers, asserts successful completion.
Parameters:
serviceName(String) - The name of the inbound webservice handlerdtoRequest(DTO_Base) - The DTO that should be serialized as the request bodyheaders(Map) - Map of header name-value pairs to add to the request
Returns: API_Base - An instance of the handler for further assertions
Since: 1.0
Example:
API_Base handler = API_InboundTestHelper.assertCallSuccessful('API_Echo', new DTO_Base(), new Map<String, String>{'Idempotency-Key' => 'abc'});setupRestContext
global static void setupRestContext()Sets up the RestContext objects of "request" and "response" to prepare for testing inbound service calls. This method initializes the request and response objects with default values.
Since: 1.0
Example:
API_InboundTestHelper.setupRestContext();setupRestContext
global static void setupRestContext(DTO_Base dtoRequest)Sets up the RestContext objects of "request" and "response" for testing purposes. This method serializes the provided DTO into the request body to simulate a real inbound request.
Parameters:
dtoRequest(DTO_Base) - The DTO that should be serialized and represents the JSON sent in a request.
Since: 1.0
Example:
API_InboundTestHelper.setupRestContext(new DTO_Base());setupRestContext
global static void setupRestContext(DTO_Base dtoRequest, Map<String, String> headers)Sets up the RestContext with a serialized DTO body and custom headers. Merges custom headers (e.g., idempotency keys, trace parents) into the RestContext request.
Parameters:
dtoRequest(DTO_Base) - The DTO that should be serialized and represents the JSON sent in a request.headers(Map) - Map of header name-value pairs to add to the request.
Since: 1.0
Example:
API_InboundTestHelper.setupRestContext(new DTO_Base(), new Map<String, String>{'Idempotency-Key' => 'abc-123'});