Test and Debug Functions
This section describes how to test and validate your function before deploying and using it.
As you're developing your function you'll want to run it locally on your development workstation to validate your function is working properly. You'll eventually also want to validate your function after it's deployed, but it's easier to catch bugs early on using local invocation.
For more details on how to run and invoke a function locally, see Invoke a Local Function.
When you create a function in your project, a default test file is generated as well. Depending on the language you specified when creating the function, the test file will use some default test frameworks. You are free to use your preferred testing frameworks instead, if needed.
For examples of tests using mocking, see the test folders for the various functions in the Functions recipe samples. Some examples:
- Test for Context_SalesforceSDK_Java Java Function
- Example of Mocking and Testing UnitOfWork in Java
See also any additional language-specific test considerations in the following docs:
If you've added your functions project to GitHub, you can Connect CI/CD Solutions to your GitHub repo to automate testing when changes are made.
Testing Your Apex Code
You'll want to ensure your Apex code that invokes your functions is properly tested. Functions cannot be invoked in an Apex test. A “Function invocations from Apex tests are not supported” exception is thrown if Apex determines that a function is being invoked during a test. Use the FunctionInvokeMock Interface and the MockFunctionInvocationFactory Class to test Apex code that invokes a function. See FunctionInvocation Example Implementation for an example of a mocked FunctionInvocation that you can use in tests.
For general information on writing Apex tests, see Apex Testing.
You can debug your function using VS Code or logs. If you're using VS Code with the Salesforce Extensions for VS Code installed, you can use extension commands to make it easy to debug local running functions.
For debugging Java functions, you may need to do some additional configuration in VS Code. See Java Functions for more details.
To debug your function in VS Code, start your function, set breakpoints in your function code, create or open a payload JSON file, and click Debug Invoke. This invokes the function and attaches the VS Code debugger to the function container.
See Invoke a Function for more details on how to start a function locally using VS Code.
Salesforce Functions generate run time log information that can be accessed via the Salesforce CLI or Functions Manager. You can generate custom log lines in your function code that help you debug your function. See Function Logging for more details.