Skip to navigation

How to perform unit testing

View as Markdown

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. 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 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
@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 methods that allow you to create authorized test users that have the right permissions to execute Docusign API methods.

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 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
// 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
// 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.

View the source code for this how-to on GitHub: Apex

Selecting unit tests

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

Next steps