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 start your AI Journey?

Table of Contents Introduction Are you tired of the same old world? Do you dream of painting landscapes with your code, composing symphonies with data, or...

The Secret Weapon: Prompt Engineering: 🪄

Table of Contents Introduction Crafting the perfect prompt is like whispering secret instructions to your AI muse. But there's no one-size-fits-all approach! Exploring different types of...

How to Singup for your own Production org?

Let's see how we can have our salesforce enterprise Salesforce Org for a 30-day Trial and use it as a Production environment. With the...

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