What is test class in Salesforce?

on

|

views

and

comments

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% code coverage 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.
  • Assert Class Methods

Watch Complete Video

Hands-On

				
					@IsTest(seeAllData=false)
public class AccountTriggerTest {
    
    /*@TestSetup // NOT A TEST METHOD
    public static void setupData(){ 
        // Used for setting up the data that will be used for the class/trigger we are writing the test case
        Account accRecord = new Account(Name = 'TEST Account');
        insert accRecord;
        Payment__c paymentRecord = new Payment__c(Account__c = accRecord.Id);
        insert paymentRecord;
        // 2
    }*/
    
    @IsTest(seeAllData=true) // NOT RECOMMENDED - 99.999999999
    private static void beforeInsert_Test(){
        List<Account> accountList = [SELECT Id, Name FROM ACCOUNT WHERE NAME = 'TEST Account'];
        Arithmatic arth = new Arithmatic();
        Integer sum = arth.sum(3, 9); 
        // Expected Outcome - 12
        // Actual Outcome - Returned(Outcome of the process) by the process
        // Validate the outcome
        Assert.areEqual(12, sum, 'The sum of the two numbers 3 & 9 are not correct!');
    }
    
    @IsTest // Test Method
    private static void beforeInsertTest(){
        // latest Way for creating test method
        Account accRecord = new Account(Name = 'TEST Account');
        insert accRecord;
        Payment__c paymentRecord = new Payment__c(Account__c = accRecord.Id);
        insert paymentRecord;
        //150th
        Test.startTest();
        // RESET
        // DML - 150
        // SOQL - 100 [SELECT STATEMENT]
        // SOQL - 20
        // SOQL ROWS [NO OF RECORDS RETURED BY SELECT STATEMENT - 50K]
        AccountTriggerHandler.handleAfterUpdate();
        AccountTriggerHandler.handleTest();
        Test.stopTest();
    }
    // Test Method
    private testMethod static void afterInsertTest(){
        // Old Way for creating test method
        Account accRecord = new Account(Name = 'TEST Account');
        insert accRecord;
        Payment__c paymentRecord = new Payment__c(Account__c = accRecord.Id);
        insert paymentRecord;
        //150th
        Test.startTest();
        AccountTriggerHandler.handleAfterUpdate();
        Test.stopTest();
    }
    
}
				
			

Resources

Amit Singh
Amit Singhhttps://www.pantherschools.com/
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
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential