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 accountList = new List();
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 accountList = new List();
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 accountList = new List();
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 accountList = new List();
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 accountList = new List();
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 accountList = new List();
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 accountList = [SELECT Id, Name, Rating, Industry, Phone, Description FROM Account LIMIT 500];
Test.startTest();
update accountList;
Test.stopTest();
}
@IsTest
private static void beforeDeleteTest(){
List accountList = [SELECT Id, Name, Rating, Industry, Phone, Description FROM Account LIMIT 500];
Test.startTest();
delete accountList;
Test.stopTest();
}
@IsTest
private static void afterUnDeleteTest(){
List 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 accountList = [SELECT Id, Name, Rating, Industry, Phone, Description FROM Account LIMIT 500];
Test.startTest(); // Test Started
AccountTriggerHandler.handleTest();
Test.stopTest();
}
}
Watch Complete Video
Resources
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Assert.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_test.htm
- https://www.pantherschools.com/database-dml-methods-sosl-in-salesforce/