Namespace
Usage
To mock Salesforce Functions testing, implement an appropriate mock response in the respond(functionName,payload) method of the FunctionInvokeMock interface. During mock testing of a Salesforce Functions, Apex runtime sends the response specified in the respond() method, rather than invoking the function itself. Appropriate success and error messages can be configured with the createSuccessResponse(invocationId,message) and createErrorResponse(invocationId,functionsErrorType,errorMsg) methods in Functions.MockFunctionInvocationFactory.
FunctionInvokeMock Methods
The following are methods for FunctionInvokeMock.
respond(functionName, payload)
Signature
public functions.FunctionInvocation respond(String functionName, String payload)
Parameters
Return Value
Type: FunctionInvocation Interface
The result of the mock call to Salesforce Functions. Appropriate responses can be generated by using the createSuccessResponse() and createErrorResponse() methods in the Functions.MockFunctionInvocationFactory class.
FunctionInvokeMock Example Implementation
This is sample implementation of the functions.FunctionInvokeMock interface.
@IsTest
public with sharing class FunctionsInvokeMockImpl implements functions.FunctionInvokeMock {
public functions.FunctionInvocation respond(String functionName, String payload) {
// return mock success response
String invocationId = '000000000000000';
String response = 'mockResponse';
return functions.MockFunctionInvocationFactory.createSuccessResponse(invocationId, response);
}
}
This example shows the minimal setup required for testing synchronous and asynchronous functions and is simplified to include both function invocations and the FunctionCallback class.
@IsTest
public with sharing class FunctionTest {
@IsTest
static void testSyncFunctionCall() {
// Set mock class to respond to function invocations
Test.setMock( functions.FunctionInvokeMock.class, new FunctionsInvokeMockInner());
functions.Function mockedFunction = functions.Function.get('example.function');
Test.startTest();
// Synchronous function call
functions.FunctionInvocation invokeResult = mockedFunction.invoke('{}');
Test.stopTest();
// Verify that the received response contains expected mock values
Assert.areEqual(functions.FunctionInvocationStatus.SUCCESS, invokeResult.getStatus());
Assert.areEqual('mockResponse', invokeResult.getResponse());
Assert.areEqual('000000000000000', invokeResult.getInvocationId());
}
@isTest
static void testAsyncFunctionCall() {
// Set mock class to respond to function invocations
Test.setMock( functions.FunctionInvokeMock.class, new FunctionsInvokeMockInner());
functions.Function mockedFunction = functions.Function.get('example.function2');
Test.startTest();
//Asynchronous function invocation with callback
mockedFunction.invoke('{}', new DemoCallback());
Test.stopTest();
// Include assertions here about the expected callback processing
}
public with sharing class DemoCallback implements functions.FunctionCallback {
public void handleResponse(functions.FunctionInvocation invokeResult) {
// Handle result of function invocation
// The callback is included in the example here for convenience
// It would normally be defined in the classes being tested
// Verify that the received response contains expected mock values
Assert.areEqual(invokeResult.getStatus(), functions.FunctionInvocationStatus.ERROR);
functions.FunctionInvocationError resultError = invokeResult.getError();
Assert.areEqual('bang', invokeResult.getError().getMessage());
Assert.areEqual('000000000000000', invokeResult.getInvocationId());
}
}
public with sharing class FunctionsInvokeMockInner implements functions.FunctionInvokeMock {
public functions.FunctionInvocation respond(String functionName, String payload) {
// return mock success response
String invocationId = '000000000000000';
if(functionName == 'example.function2') {
return functions.MockFunctionInvocationFactory.createErrorResponse(
invocationId,
functions.FunctionErrorType.FUNCTION_EXCEPTION,
'bang');
}
String response = 'mockResponse';
return functions.MockFunctionInvocationFactory.createSuccessResponse(invocationId, response);
}
}
}