---
schema_version: "1"
id: 1181832bcd34a972bdfebb9edc203cbc6ff95b3c3dbd859e1ebcf95736bfdc1f
title: FunctionInvokeMock Interface
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_interface_functions_FunctionInvokeMock.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_functions_FunctionInvokeMock.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 0f9ebe9da1bc27c8748b9647c75c05747c575ae8c63808dc37f7c7cec481727d
description: Use the FunctionInvokeMock interface to mock Salesforce Functions responses during testing.The mock response implemented in the Functions.FunctionInvokeMock interface. The response is sent by Apex runtime when the Test.setMock() method is called.
---
# FunctionInvokeMock Interface

Use the FunctionInvokeMock interface to mock Salesforce Functions responses during testing.

## Namespace

[functions](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_functions.htm "The Functions namespace provides classes and methods used to invoke and manage Salesforce Functions.")

## 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_functions_FunctionInvokeMock.htm#apex_functions_FunctionInvokeMock_methods)**
- **[FunctionInvokeMock Example Implementation](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_functions_FunctionInvokeMock.htm#apex_interface_functions_FunctionInvokeMock_Example)**

## FunctionInvokeMock Methods

The following are methods for FunctionInvokeMock.

- **[respond(functionName, payload)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_functions_FunctionInvokeMock.htm#apex_functions_FunctionInvokeMock_respond)**  
    The mock response implemented in the Functions.FunctionInvokeMock interface. The response is sent by Apex runtime when the Test.setMock() method is called.

### respond(functionName, payload)

The mock response implemented in the Functions.FunctionInvokeMock interface. The response is sent by Apex runtime when the Test.setMock() method is called.

#### Signature

public functions.FunctionInvocation respond(String functionName, String payload)

#### Parameters

- **functionName**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.") The name of the Salesforce Function and the Functions Project that the Function is part of. The format of the parameter string is “project name.function name”.
- **payload**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.") The JSON-format payload data that is passed to the Function.

#### Return Value

Type: [FunctionInvocation Interface](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_functions_FunctionInvocation.htm#apex_interface_functions_FunctionInvocation "Use FunctionInvocation to get the status and results of a synchronous or asynchronous Function invocation.")

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);       
            }
        }
    
    
    }