Newer Version Available
FunctionInvokeMock Interface
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.
1@isTest
2public class FunctionsInvokeMockImpl implements functions.FunctionInvokeMock {
3 public functions.FunctionInvocation respond(String functionName, String payload) {
4 // return mock success response
5 String invocationId = '000000000000000';
6 String response = 'mockResponse';
7 return functions.MockFunctionInvocationFactory.createSuccessResponse(invocationId, response);
8 }
9}
10This example shows the minimal setup required for testing synchronous and asynchronous functions and is simplified to include both function invocations and the FunctionCallback class.
1@isTest
2public class FunctionTest {
3 @isTest
4 static void testSyncFunctionCall() {
5 // Set mock class to respond to function invocations
6 Test.setMock( functions.FunctionInvokeMock.class, new FunctionsInvokeMockInner());
7
8 functions.Function mockedFunction = functions.Function.get('example.function');
9
10 Test.startTest();
11 // Synchronous function call
12 functions.FunctionInvocation invokeResult = mockedFunction.invoke('{}');
13 Test.stopTest();
14
15 // Verify that the received response contains expected mock values
16 System.assertEquals(functions.FunctionInvocationStatus.SUCCESS, invokeResult.getStatus());
17 System.assertEquals('mockResponse', invokeResult.getResponse());
18 System.assertEquals('000000000000000', invokeResult.getInvocationId());
19
20
21 }
22
23 @isTest
24 static void testAsyncFunctionCall() {
25 // Set mock class to respond to function invocations
26 Test.setMock( functions.FunctionInvokeMock.class, new FunctionsInvokeMockInner());
27
28 functions.Function mockedFunction = functions.Function.get('example.function2');
29
30 Test.startTest();
31 //Asynchronous function invocation with callback
32 mockedFunction.invoke('{}', new DemoCallback());
33 Test.stopTest();
34 // Include assertions here about the expected callback processing
35 }
36
37 public class DemoCallback implements functions.FunctionCallback {
38 public void handleResponse(functions.FunctionInvocation invokeResult) {
39 // Handle result of function invocation
40 // The callback is included in the example here for convenience
41 // It would normally be defined in the classes being tested
42
43 // Verify that the received response contains expected mock values
44 System.assertEquals(invokeResult.getStatus(), functions.FunctionInvocationStatus.ERROR);
45 functions.FunctionInvocationError resultError = invokeResult.getError();
46 System.assertEquals('bang', invokeResult.getError().getMessage());
47 System.assertEquals('000000000000000', invokeResult.getInvocationId());
48
49 }
50 }
51
52 public class FunctionsInvokeMockInner implements functions.FunctionInvokeMock {
53 public functions.FunctionInvocation respond(String functionName, String payload) {
54 // return mock success response
55 String invocationId = '000000000000000';
56
57 if(functionName == 'example.function2') {
58 return functions.MockFunctionInvocationFactory.createErrorResponse(
59 invocationId,
60 functions.FunctionErrorType.FUNCTION_EXCEPTION,
61 'bang');
62 }
63
64 String response = 'mockResponse';
65 return functions.MockFunctionInvocationFactory.createSuccessResponse(invocationId, response);
66 }
67 }
68
69
70 }