Test Classes In Apex Salesforce | The Developer Guide
Best Practices of Test Class in Salesforce
@TestVisible - To access private member in Test Class
Test.LoadData - Creating test data without code
System.RunAs - Tesing User Context
Test.StartTest & Test.StopTest - Running code within limit
Example Of Test Class
Task 1:-
Update Contact Email Address and write its Test Class
Trigger Code:-
trigger UpdateContactEmailTrigger on Account (after update) {
if(trigger.isUpdate){
UpadteContactEmailHandler.updateContactEmailAddress(trigger.new,trigger.oldMap);
}
}
Don't forget to check out: All About Apex Testing in Salesforce
Apex Class Code:-
public class UpadteContactEmailHandler {
public static void updateContactEmailAddress(List<Account> lstNew, Map<Id,Account> mapOld){
Set<Id> accIds = new Set<Id>();
system.debug('===oldmap==='+mapOld);
system.debug('===lstNew==='+lstNew);
for(Account acc : lstNew){
system.debug('===oldmap email==='+mapOld.get(acc.Id).Account_Email__c);
system.debug('===lstNew=email=='+acc.Account_Email__c);
if(acc.Account_Email__c != mapOld.get(acc.Id).Account_Email__c && acc.Account_Email__c !=null){
accIds.add(acc.Id);
}
}
system.debug('===accIds=='+accIds);
if(!accIds.isEmpty()){
List<Contact> lstContact = [Select ID,
Account.Account_Email__c,
Email
FROM
Contact
where
AccountID IN :accIds];
system.debug('===lstContact=='+lstContact);
List<Contact> lstUpdateCon = new List<Contact>();
for(Contact con : lstContact){
if(con.Email != con.Account.Account_Email__c){
con.Email = con.Account.Account_Email__c;
lstUpdateCon.add(con);
}
else{
con.Email =null;
lstUpdateCon.add(con);
}
}
if(!lstUpdateCon.isEmpty()){
update lstUpdateCon;
}
}
}
}
Test Class Of This Code:-
@istest
public class UpadteContactEmailHandler_Test {
@istest static void updateContactEmailAddressTest(){
List<Account> listOgAccount = new List<Account>();
Account accountObj= new Account();
accountObj.Name='Test Account1';
accountObj.Account_Email__c='test@gmail.com';
listOgAccount.add(accountObj);
insert listOgAccount;
Contact conObj =new Contact(LastName='Test Contact', Email='test@gmail.com', Accountid=accountObj.Id);
insert conObj;
accountObj.Account_Email__c='test1@gmail.com';
update accountObj;
/* Map<id,Account> oldmap = new Map<id,Account>();
Map<id,Contact> newmap = new Map<id,Contact>();
Account acct = new Account(Name='Test Account',Account_Email__c='accounttest@gmail.com');
insert acct;
oldmap.put(acct.id, acct);
contact con = new contact();
con.accountid = acct.id;
con.LastName = 'TestContact';
insert con;
Test.startTest();
con.Email = 'contest@gmail.com';
update con;
newmap.put(con.id, con);
//UpadteContactEmailHandler.updateContactEmailAddress(oldmap,newmap);
Test.stopTest(); */
}
}
Task 2:-
Concatenate Contact Name on Related Account Name
Trigger Code:-
trigger ContcatenateContactNameOnAccount on Contact (after insert, after update, after delete, after undelete) {
if(trigger.isAfter){
if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete){
ContcatenateContactNameOnAccountClass.updateContactOnAccount(trigger.new);
}
if(trigger.isDelete || trigger.isUpdate){
ContcatenateContactNameOnAccountClass.updateContactOnAccount(trigger.old);
}
}
}
Check out another amazing blog by Arpit here: All About Batch Class in Salesforce | The Developer Guide
Apex Class Code:-
public class ContcatenateContactNameOnAccountClass {
public static void updateContactOnAccount(List<Contact> listOfContacts){
//receiving list of Contact from trigger as parameter
Set<Id> accountSet = new Set<Id>();
List<Account> listOfAccountToBeUpdate = new List<Account>();
// Add each contact's AccountId in a set
for(Contact con : listOfContacts) {
accountSet.add(con.AccountId);
}
//Query and inner query to get id and all contact from account and name from child contact
List<Account> listOfAccountWithContact = [SELECT Id, All_Contacts__c,
(SELECT Id, Name FROM Contacts ORDER BY LastModifiedDate DESC)
FROM Account Where id IN :accountSet];
for(Account acc : listOfAccountWithContact){
Account newAccount = new Account();
String strAllContacts = '';
for(Contact con :acc.Contacts){//iterating each contact to add their name.
strAllContacts= strAllContacts + con.Name + ' , ';
}
newAccount.All_Contacts__c = strAllContacts.removeEnd(' , ');
newAccount.Id = acc.id;
listOfAccountToBeUpdate.add(newAccount);//adding to the list
}
if(!listOfAccountToBeUpdate.isEmpty()) {
update listOfAccountToBeUpdate;
}
}
/* public static List<Opportunity> createOpportunity(String accountId, Integer numberOfRecords){
List<Opportunity> listOfOpportunity = new List<Opportunity>();
for(Integer i=0;i<numberOfRecords;i++){
Opportunity newOpportunity = new Opportunity();
newOpportunity.AccountId = accountId;
listOfOpportunity.add(newOpportunity);
}
if(!listOfOpportunity.isEmpty()){
insert listOfOpportunity;
}
return listOfOpportunity;
}
List<Opportunity> listOfOpportunity= DataFactory.createOpportunity(accountObj.id, 4);
insert listOfOpportunity;
*/
}
Test Class Code:-
@istest
public class ContcatConnNameOnAccountClass_Test {
@istest static void CheckAllConcatenateContactWithAccount(){
List<Account> listOfAccount = new List<Account>();
Account accountObj = new Account(Name='Test Account 1');
listOfAccount.add(accountObj);
Account accountConObj = new Account(Name='Test Account 2');
listOfAccount.add(accountConObj);
Account accountCon1Obj = new Account(Name='Test Account 3');
listOfAccount.add(accountCon1Obj);
insert listOfAccount;
List<Contact> listOfContact = new List<Contact>();
Contact conObj= new Contact(LastName='TestContact1', Accountid=accountObj.id);
listOfContact.add(conObj);
Contact con1Obj= new Contact(LastName='TestContact2', Accountid=accountConObj.id);
listOfContact.add(con1Obj);
Contact con2Obj= new Contact(LastName='TestContact3', Accountid=accountCon1Obj.id);
listOfContact.add(con2Obj);
insert listOfContact;
conObj.AccountId=accountConObj.id;
update conObj;
}
}

Responses