LEVEL UP YOUR SALESFORCE UNIT
TESTING
Gordon Bockus
Austin Salesforce Developer Group
March 2017
WHO AM I?
• Co-Founder at Wagion
• Lightning Development
• Uniting testing to the Core
TOPICS
• What is a Unit Test?
• Architect to Enable Tests
• Stub API to the Rescue
• How to Run Tests
• LightningTestingService
WHAT IS A UNIT TEST
Unit testing is a software development process
in which the smallest testable parts of
an application, called units, are individually and
independently scrutinized for proper operation.
MOTORCYCLE EXAMPLE
Test this
Not this
UNIT TESTS ARE NOT
SYSTEM TESTS
• Only test the code in the
method under test
• Avoid complex test setup
• Not just the happy path
• Cover all paths in the method
• Validation
• Success
• Failure
SALESFORCE UNIT TESTS
• ClassNameTest
• testMethodNameInfo
• Test.startTest/Test.stopTest
• isTest(SeeAllData=true) Annotation
• Keep it DRY (testUtilities)
ARCHITECT TO ENABLE
TESTS
• Controllers/Triggers -
Specific to task at
hand
• Services - Business
Logic
• Dao - Database
Interactions
DEMO INTRODUCTION
• https://github.com/gbockus/SFUnitTestLevel
Up
• Lightning Components
• Good Component
• Bad Component
STUB API TO THE RESCUE
• Stub out instance
methods
• Focus only on code
under test
• Apex Stub API DOC
STUB API LIMITATIONS
• Not stub-able
• Static methods
• Private methods
• Triggers
• Inner classes
• See documentation for full list
STUB API
IMPLEMENTATION
• AbstractMock
• AccountServiceMock/ContactServiceMock
(abstract)
• AccountServiceSuccessMock/AccountServic
eExceptionMock/etc (instances)
ABSTRACT MOCK
ACCOUNT SERVICE MOCK
STUBBED CLASS UPDATES
UNIT TESTS IMPROVED
• GoodControllerTest
TAKE AWAYS
• Pros
• True unit tests
• Testing actual logic is easier
• Less system wide concerns when writing test
• Cons
• Boiler plate code
CHALLENGE
• Deploy code in the repo to a development
org and implement missing tests
RUNNING TESTS
• IDE
• Salesforce/Developer console
• Ant Migration Tool
• jsforce
• SFDX - sfdx force:apex:test:run
LIGHTNING TESTING
SERVICE
• Allows you to write unit tests for lightning javascript
code using modern tooling
• Jasmine
• Mocha
• Install via SFDX or unmanaged package
• Lightning Testing Service
THANK YOU
• https://github.com/gbockus/SFUnitTestLevel
Up
• Apex Stub API
• Salesforce Ant Migration Tool
• jsforce
• Lightning Testing Service

Level Up Your Salesforce Unit Testing

Editor's Notes