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
What is Apex Testing?
It is Apex code with the sole purpose of testing the production. Apex test code is used to ensure the production test code works properly.
Salesforce Requirements
Code coverage is a metric used to determine what percentage of the production Apex code is executed by the Apex test code. For example, if there are 4 lines of production Apex code and the Apex test code executes 2 lines of the production code, then there is 50% code coverage.
Salesforce requires that at least 75% of the overall production Apex code is covered by Apex test code. Without it, you can’t deploy your production Apex code to production. Similarly, you can’t create a package for an AppExchange application or an unlocked package. My guess is that Salesforce requires this so that whenever they do a new release, they can run all the tests from all their customers to see if anything breaks or not, and resolve it before the upgrade is released.
The 75% requirement is the primary one but there is actually a second one. You must also have at least 1% code coverage on every Apex Trigger. That means you have to have test code for each Trigger no matter what, whereas with Non-Trigger code, you may not have any test code for an Apex class and it can still deploy to production because the overall Apex code coverage is at least 75%.