Salesforce Apex Testing Essentials
-
Welcome to Salesforce Ben Courses1 Topic
-
Introduction5 Topics
-
Basics9 Topics
-
Testing Triggers10 Topics
-
Test Data Creation10 Topics
-
Overview
-
DON’T Use A Single Test Data Class
-
SObject Test Data Framework
-
Create SObjectTestData Class
-
Add Default Account Source to AccountTestData
-
Implementing an SObject Test Data Class
-
Implement OpportunityTestData Class
-
Add Create and Insert Object Functions
-
Full OpportunityTestData Class
-
Test Data Creation Exercises
-
Overview
-
Testing Asynchronous Apex7 Topics
-
Best Practices7 Topics
-
Wrap Up1 Topic
Benefits
Using Apex Tests has numerous benefits such as regression testing, identifying bugs earlier, functioning as client code, and building confidence.
Regression Testing
First and foremost, the primary benefit of Apex tests is that it provides regression testing. That means as changes are made to the production code, you can run the Apex Tests and see if anything breaks. If it does, then you can troubleshoot the changes and correct them as needed. This is much faster than having people test the code and manually testing everything. People still need to test, but often they can focus on other aspects that aren’t as easily covered by Apex tests such as user experience.
Identifies Bugs Earlier
If the Apex Tests are regularly and automatically executed, they can identify issues earlier in the development process and be corrected sooner. The later that a bug is discovered, the more expensive it is to fix. It is much cheaper to fix it during development than after it is deployed to production. Some estimate that it is 100 times more expensive to fix in production than when it is in development.
Salesforce does not provide this capability out-of-the-box; it would be nice! However, you can run their tests automatically by using the Salesforce CLI to run them on a server somewhere and report the results. There are third-party DevOps tools that can do this too. This is often done as part of a release, aka DevOps process which is outside the scope of this course.
Client Code
While ensuring production code works is the primary benefit, Apex Test code also demonstrates how to use the production code so it acts as “client code”. This allows developers to see how to work with the production code when they are learning the code base.
Confidence
Have you or another developer made changes to a code base and something unexpectedly breaks in another part of the code? Yeah… me too. With sufficient test code that is regularly executed and passing, it builds confidence that the production code works!
It doesn’t guarantee that there aren’t bugs BUT it helps minimize them and provides a first line of defense.