What should be there in a Test Classes?

on

|

views

and

comments

Table of Contents

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.

Introduction

Test Class Must need to have

  • @testSetup Method
  • Test.startTest() & Test.stopTest()
  • Assert Class methods
  • Use try & catch where applicable
  • Positive & Negative Scenario
  • Testing for Bulk Records

Test Class Good to have

  • runAs
  • Utility Class

Hands-On

				
					@IsTest(seeAllData=false)
public class AccountTriggerTest {
    
    @IsTest
    private static void beforeInsertTest(){
        List<Account> accountList = new List<Account>();
        Account accountRecord = new Account();
        accountRecord.Name = 'Test Account';
        accountRecord.Phone = '9087654321';
        accountList.add(accountRecord);
        
        Account accountRecord1 = new Account();
        accountRecord1.Name = 'Test Account';
        accountRecord1.Phone = '9087654321';
        accountRecord1.ShippingStreet = '123 Main Street';
        accountList.add(accountRecord1);
        
        Account accountRecord2 = new Account();
        accountRecord2.Name = 'Test Account';
        //accountRecord2.Phone = '9087654321';
        accountRecord2.ShippingStreet = '123 Main Street';
        accountRecord2.ShippingCity = 'Florida';
        accountList.add(accountRecord2);
        
        Account accountRecord3 = new Account();
        accountRecord3.Name = 'Test Account';
        //accountRecord3.Phone = '9087654321';
        accountRecord3.ShippingStreet = '123 Main Street';
        accountRecord3.ShippingCity = 'Florida';
        
        Test.startTest();
            try{
                insert accountList;
                insert accountRecord3;// 1 Times Trigger Fired
            }catch(System.DmlException ex){
                System.debug('Exception Found!');
            }
        Test.stopTest();
    }
    
    @IsTest
    private static void beforeUpdateTest(){
        List<Account> accountList = new List<Account>();
        Account accountRecord = new Account();
        accountRecord.Name = 'Test Account';
        accountRecord.Phone = '9087654321';
        accountList.add(accountRecord);
        
        Account accountRecord1 = new Account();
        accountRecord1.Name = 'Test Account';
        accountRecord1.Phone = '9087654321';
        accountRecord1.ShippingStreet = '123 Main Street';
        accountList.add(accountRecord1);
        
        Account accountRecord2 = new Account();
        accountRecord2.Name = 'Test Account';
        //accountRecord2.Phone = '9087654321';
        accountRecord2.ShippingStreet = '123 Main Street';
        accountRecord2.ShippingCity = 'Florida';
        accountList.add(accountRecord2);
        
        insert accountList;
        Test.startTest();
            update accountList;
        Test.stopTest();
        
        
    }
    @IsTest
    private static void beforeDeleteTest(){
        List<Account> accountList = new List<Account>();
        Account accountRecord = new Account();
        accountRecord.Name = 'Test Account';
        accountRecord.Phone = '9087654321';
        accountList.add(accountRecord);
        
        Account accountRecord1 = new Account();
        accountRecord1.Name = 'Test Account';
        accountRecord1.Phone = '9087654321';
        accountRecord1.ShippingStreet = '123 Main Street';
        accountList.add(accountRecord1);
        
        Account accountRecord2 = new Account();
        accountRecord2.Name = 'Test Account';
        //accountRecord2.Phone = '9087654321';
        accountRecord2.ShippingStreet = '123 Main Street';
        accountRecord2.ShippingCity = 'Florida';
        accountList.add(accountRecord2);
        
        insert accountList;
        Test.startTest();
        delete accountList;
        Test.stopTest();
    }
    
    @IsTest
    private static void afterUnDeleteTest(){
        List<Account> accountList = new List<Account>();
        Account accountRecord = new Account();
        accountRecord.Name = 'Test Account';
        accountRecord.Phone = '9087654321';
        accountList.add(accountRecord);
        
        Account accountRecord1 = new Account();
        accountRecord1.Name = 'Test Account';
        accountRecord1.Phone = '9087654321';
        accountRecord1.ShippingStreet = '123 Main Street';
        accountList.add(accountRecord1);
        
        Account accountRecord2 = new Account();
        accountRecord2.Name = 'Test Account';
        //accountRecord2.Phone = '9087654321';
        accountRecord2.ShippingStreet = '123 Main Street';
        accountRecord2.ShippingCity = 'Florida';
        accountList.add(accountRecord2);
        
        insert accountList;
        delete accountList;
        Test.startTest();
        undelete accountList;
        Test.stopTest();
    }
    
    @IsTest
    private static void handleTest(){
        List<Account> accountList = new List<Account>();
        Account accountRecord = new Account();
        accountRecord.Name = 'Test Account';
        accountRecord.Phone = '9087654321';
        accountList.add(accountRecord);
        
        Account accountRecord1 = new Account();
        accountRecord1.Name = 'Test Account';
        accountRecord1.Phone = '9087654321';
        accountRecord1.ShippingStreet = '123 Main Street';
        accountList.add(accountRecord1);
        
        Account accountRecord2 = new Account();
        accountRecord2.Name = 'Test Account';
        //accountRecord2.Phone = '9087654321';
        accountRecord2.ShippingStreet = '123 Main Street';
        accountRecord2.ShippingCity = 'Florida';
        accountList.add(accountRecord2);
        
        insert accountList;
        
        Test.startTest(); // Test Started
            AccountTriggerHandler.handleTest();
        Test.stopTest();
    }
}
				
			

Hands-On

				
					@IsTest(seeAllData=false)
public class AccountTriggerTest {
    
    @TestSetup
    private static void setupData(){
        List<Account> accountList = new List<Account>();
        Account accountRecord = new Account();
        accountRecord.Name = 'Test Account';
        accountRecord.Phone = '9087654321';
        accountList.add(accountRecord);
        
        Account accountRecord1 = new Account();
        accountRecord1.Name = 'Test Account';
        accountRecord1.Phone = '9087654321';
        accountRecord1.ShippingStreet = '123 Main Street';
        accountList.add(accountRecord1);
        
        Account accountRecord2 = new Account();
        accountRecord2.Name = 'Test Account';
        //accountRecord2.Phone = '9087654321';
        accountRecord2.ShippingStreet = '123 Main Street';
        accountRecord2.ShippingCity = 'Florida';
        accountList.add(accountRecord2);
        insert accountList;
    }
    
    @IsTest
    private static void beforeInsertTest(){
        
        Account accountRecord3 = new Account();
        accountRecord3.Name = 'Test Account';
        //accountRecord3.Phone = '9087654321';
        accountRecord3.ShippingStreet = '123 Main Street';
        accountRecord3.ShippingCity = 'Florida';
        
        Test.startTest();
            try{
                insert accountRecord3;// 1 Times Trigger Fired
            }catch(System.DmlException ex){
                System.debug('Exception Found!');
            }
        Test.stopTest();
    }
    
    @IsTest
    private static void beforeUpdateTest(){
        List<Account> accountList = [SELECT Id, Name, Rating, Industry, Phone, Description FROM Account LIMIT 500];
        Test.startTest();
            update accountList;
        Test.stopTest();
        
        
    }
    @IsTest
    private static void beforeDeleteTest(){
        List<Account> accountList = [SELECT Id, Name, Rating, Industry, Phone, Description FROM Account LIMIT 500];
        Test.startTest();
        delete accountList;
        Test.stopTest();
    }
    
    @IsTest
    private static void afterUnDeleteTest(){
        List<Account> accountList = [SELECT Id, Name, Rating, Industry, Phone, Description FROM Account LIMIT 500];
        delete accountList;
        Test.startTest();
        undelete accountList;
        Test.stopTest();
    }
    
    @IsTest
    private static void handleTest(){
        List<Account> accountList = [SELECT Id, Name, Rating, Industry, Phone, Description FROM Account LIMIT 500];
        Test.startTest(); // Test Started
            AccountTriggerHandler.handleTest();
        Test.stopTest();
    }
}
				
			

Watch Complete Video

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