---
schema_version: "1"
id: b6d4fe4c62697a2d73572fcc0162779291be40df4f168b4d5088c627d59380b3
title: Test and Debug Functions
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/salesforce-functions/functions/guide/dev-guide/test-and-validate.md
canonical_url: https://developer.salesforce.com/docs/platform/functions/guide/test-and-validate.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 5adad19756bc34b93fb82049ccb9d9922cc28bf8374d508cf44d672ae2972901
products:
  - Salesforce Functions
---
# Test and Debug Functions

This section describes how to test and validate your function before deploying and using it.

## Run Your Function Locally

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](https://developer.salesforce.com/docs/platform/functions/guide/invoke-local.md).

## Test Your 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.

When writing tests for your function, you might need to mock code that accesses your org. Frameworks like [Sinon](https://sinonjs.org/) or [Jest](https://jestjs.io/) (for JavaScript) and [Mokito](https://site.mockito.org/) (for Java) may help when writing mocks.

For examples of tests using mocking, see the test folders for the various functions in the [Functions recipe samples](https://github.com/trailheadapps/functions-recipes). Some examples:

- [Test for Context_SalesforceSDK_JS JavaScript Function](https://github.com/trailheadapps/functions-recipes/blob/main/functions/03_Context_SalesforceSDK_JS/test/index.test.js)
- [Test for Context_SalesforceSDK_Java Java Function](https://github.com/trailheadapps/functions-recipes/blob/main/functions/03_Context_SalesforceSDK_Java/src/test/java/com/salesforce/functions/recipes/FunctionTest.java)
- [Example of Mocking and Testing UnitOfWork in JavaScript](https://github.com/trailheadapps/functions-recipes/blob/main/functions/03_Context_UnitOfWork_JS/test/index.test.js)
- [Example of Mocking and Testing UnitOfWork in Java](https://github.com/trailheadapps/functions-recipes/blob/main/functions/03_Context_UnitOfWork_Java/src/test/java/com/salesforce/functions/recipes/FunctionTest.java)

See also any additional language-specific test considerations in the following docs:

  <!-- - [JavaScript Functions](https://developer.salesforce.com/docs/platform/functions/guide/javascript-functions.md#test-considerations) -->

- [Java Functions](https://developer.salesforce.com/docs/platform/functions/guide/java-functions.md#test-considerations)

### Automate Testing

If you've added your functions project to GitHub, you can [Connect CI/CD Solutions](https://developer.salesforce.com/docs/platform/functions/guide/dev-alm-workflow.html#add-continuous-integrationcontinuous-deployment) 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_functions_FunctionInvokeMock.htm#apex_interface_functions_FunctionInvokeMock) and the [MockFunctionInvocationFactory Class](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_functions_MockFunctionInvocationFactory.htm#apex_class_functions_MockFunctionInvocationFactory) to test Apex code that invokes a function. See [FunctionInvocation Example Implementation](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_functions_FunctionInvocation.htm#apex_interface_functions_FunctionInvocation_Example) for an example of a mocked FunctionInvocation that you can use in tests.

For general information on writing Apex tests, see [Apex Testing](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm).

## Use Logs to Debug

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](https://developer.salesforce.com/docs/platform/functions/guide/function-logging.md) for more details.
