> You are reading the Docusign Developer Documentation. For clean Markdown of any page, append .md to the page URL.
>
> Documentation indexes by section:
> - APIs and SDKs: https://docusign.docs.buildwithfern.com/docs/llms.txt
> - Platform 101: https://docusign.docs.buildwithfern.com/platform/llms.txt
> - Extension Apps: https://docusign.docs.buildwithfern.com/extension-apps/llms.txt
> - Docusign CLI: https://docusign.docs.buildwithfern.com/docusign-cli/llms.txt
>
> Authentication (OAuth, JWT Grant, M2M): https://docusign.docs.buildwithfern.com/platform/auth
> Webhooks and Connect: https://docusign.docs.buildwithfern.com/platform/webhooks
>
> OpenAPI specifications: https://docusign.docs.buildwithfern.com/openapi.json
> MCP server: https://docusign.docs.buildwithfern.com/_mcp/server

# How to perform unit testing

> Learn how to unit test your Salesforce integration code with the Apex Toolkit.

This how-to shows you how to integrate the Apex Toolkit into your unit tests. According to best practices and mandated by Salesforce in some cases, your Apex code must be covered by [unit tests](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unit_tests.htm). When dealing with third-party code, this can be a difficult proposition. We strive to simplify this process via the Apex Toolkit. To that end, we have exposed a number of test utility methods and web service mocks.

## Prerequisites

1. Install the [Docusign eSignature for Salesforce](/docs/salesforce/how-to/apex-toolkit-install)  managed package.
2. Create a subdomain for your Salesforce organization by enabling "My Domain."
3. Select the **Docusign Setup** tab and complete the login step.

## Step 1. Define a private class and method

Create a new private class and define your test method inside of it. Include the `@IsTest` annotation on the class and method to indicate that your class will be used as a unit test.

**`Apex`**

```java title="Apex"
@IsTest
public with sharing class UnitTestExample {

    @IsTest
    public static void testSendEnvelope() {
   }
}
```

## Step 2. Create test objects and data

The Apex Toolkit exposes a number of [dfsle.UserMock](/docs/salesforce/apex-toolkit-reference/usermock.html) methods that allow you to create authorized test users that have the right permissions to execute Docusign API methods.

> **Note**
>
> Only one Docusign test user may be used with each unit test.

The Apex Toolkit also allows you to mock responses from Docusign web services using the [dfsle.ESignatureAPIMock](/docs/salesforce/apex-toolkit-reference/esignatureapimock.html) class. The accompanying code demonstrates how to create a mock Docusign sender and mock the eSignature API. It also shows how to create an envelope with a document and recipient to use in your test. You should copy this code into the `testSendEnvelope` method that you defined in the previous step.

**`Apex`**

```java title="Apex"
// Mock the DocuSign eSignature API
dfsle.TestUtils.setMock(new dfsle.ESignatureAPIMock());

// Run the test as a DocuSign Sender. This is required by dfsle.EnvelopeService.sendEnvelope
System.runAs(dfsle.UserMock.createDocuSignSender()) {

// Create test data
Account myAccount = new Account(Name = 'Test Account');
insert myAccount;

Contact myContact = new Contact(
    AccountId = myAccount.Id,
    FirstName = 'Test',
    LastName = 'Contact',
    Phone = '555-1234-5678',
    Email = 'test.contact@example.com');
insert myContact;

// Create a test envelope with one document and one recipient
dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(new dfsle.Entity(myAccount.Id))
    .withDocuments(new List<dfsle.Document> {
        dfsle.Document.fromTemplate(
            dfsle.UUID.randomUUID(),
            'test template')
    })
    .withRecipients(new List<dfsle.Recipient> {
        dfsle.Recipient.fromSource(
            myContact.FirstName + ' ' + myContact.LastName,
            myContact.Email,
            myContact.Phone,
            'Signer 1',
            new dfsle.Entity(myContact.Id))
    });
      }
```

## Step 3. Perform the test and verify the results

The last step in defining your test method is using the `Test.startTest` and `Test.stopTest` methods to perform the test. Then you'll use the `System.assertNotEquals` method to ensure that the results of your test were not `null`.

The accompanying code shows how to execute your test. You should copy this code inside the `testSendEnvelope` method that you defined in Step 1, after the code from the previous step.

**`Apex`**

```java title="Apex"
// Perform the test
Test.startTest();
dfsle.Envelope myResult = dfsle.EnvelopeService.sendEnvelope(myEnvelope, true);
Test.stopTest();

// Verify the results
System.assertNotEquals(null, myResult);
```

## Step 4. Run the unit test

To run your test, go to the Developer Console, open the **Test** menu, then select **New Run**. A table will appear that will allow you to select specific classes and tests. Select your newly created test in the left column and select **Add Selected**. Then select **Run** in the bottom right corner.

> **Note**
>
> View the source code for this how-to on GitHub: [Apex](https://github.com/docusign/code-examples-apex/blob/master/force-app/main/default/classes/UnitTestExample.cls)

![Selecting unit tests](/_fern-files/_img/a9d5b269099d263265a74bc95ec56ddf3239a9e53f866c63aeb4a9ffc090c627.webp)

## Expected output

When your unit test runs, you will see new test results data accrue on the **Logs** tab similar to this screen. Double-click a log record to see the execution event details for that test run.

![Sample response output](/_fern-files/_img/a4516984f4ddc4360382166b2ea063f909b7206bbc864d81ff9cd8b54c3311f4.webp)

## Next steps

* See all available Apex Toolkit [How-to guides](/docs/salesforce/how-to).
* See [How to send an envelope](/docs/salesforce/how-to/send-envelope).