How to perform unit testing
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
- Install the Docusign eSignature for Salesforce managed package.
- Create a subdomain for your Salesforce organization by enabling “My Domain.”
- 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.
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.
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.
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
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.
Next steps
- See all available Apex Toolkit How-to guides.
- See How to send an envelope.