In summary, test classes are an essential part of the development process in Salesforce. They help to ensure code quality, catch errors early, meet code coverage requirements, facilitate collaboration, and facilitate code maintenance.
Table of Contents
Introduction
In Salesforce, a test class is used to test the functionality of Apex classes and triggers. The purpose of writing a test class is to ensure that your code works as expected and that it meets the requirements of your business logic.
Test classes help you to catch errors before they occur in production, so it is crucial to write good test classes.
It’s important to note that Salesforce requires a minimum of 75% codecoverage for all Apex classes and triggers.
This means that at least 75% of the lines of code in your classes and triggers must be covered by test methods.
Benefits of test Class
Ensuring code quality: Test classes help to ensure that your code works as intended and meets the requirements of your business logic.
Positive test Cases
Negative Test Cases
Code Coverage
Bulk Record Testing
Catching errors early: Test classes help to catch errors early in the development process before they reach production. This saves time and reduces the risk of costly errors that can impact business processes.
Resources Required
Time
Man Power
$
1 Hour of time spent on development
Development Phase – Immediately fix ( 2 times ) – 2*4
Testing Phase – 4 times – 4*4
UAT (User Acceptance Testing) – 8 times – 8*4
System Integration Phase – 25 times – 25*5
Production – 100 times – 100*4
Meeting code coverage requirements: Test classes are required in Salesforce to meet code coverage requirements.
Facilitating collaboration: Test classes can help facilitate collaboration between developers and QA teams.
Facilitating code maintenance: Test classes can help with code maintenance by providing a way to test changes made to existing code.
Summary
In summary, test classes are an essential part of the development process in Salesforce. They help to ensure code quality, catch errors early, meet code coverage requirements, facilitate collaboration, and facilitate code maintenance.
Important Points
@IsTest must be used at the top of your Apex Class (Test Class)
@IsTest shall be used at the top of your test method OR testMethod keyword shall be used while creating the test method
Each test class must need to have at least 1 test method
Important Annotation of Test Class
@isTest – If we use this annotation at the top of Apex Class then the class will act as Test Class AND – If we use this annotation at the top of Apex method then the class will act as Test method inside the Test class.
@testSetup – This method always gets executed before any test method is executed. This method is used to prepare all the data that is required for the complete test class.
We used to create the common data that is required across all the test cases inside that test class
Test.startTest() & Test.stopTest() – These methods are very important as we will always test our code within these two methods. These methods provide a Fresh set of governor limits for the execution of our business logic.
System.runAs(User) – To run a certain piece of code in the user context
Positive test Cases
Negative Test Cases
Business Logic
@testVisible – This annotation will be used in the class on top of the private variable or method to make that variable or method visible to Salesforce.
@isTest(SeeAllData=false) – RECOMMENDED – If we use this annotation on top of the Apex Class or Test Class method then you have to create the object that is being used in the complete process.
@isTest(SeeAllData=true) – NOT RECOMMENDED – If we use this annotation on top of the Apex Class or Test class then the test class will use read the data from Salesforce itself. This will start creating the problem when you are trying to deploy the code to prod.
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion