Test Classes In Apex

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) {    

dont miss out iconDon'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>();
        for(Account acc : lstNew){
            system.debug('===oldmap email==='+mapOld.get(acc.Id).Account_Email__c);
            if(acc.Account_Email__c != mapOld.get(acc.Id).Account_Email__c && acc.Account_Email__c !=null){            
            List<Contact> lstContact = [Select ID, 
                                    AccountID IN :accIds];        
            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;
                    con.Email =null; 
                update lstUpdateCon;

Test Class Of This Code:-

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='[email protected]';
        insert listOgAccount;        
        Contact conObj =new Contact(LastName='Test Contact', Email='[email protected]', Accountid=accountObj.Id);
        insert conObj;        
        accountObj.Account_Email__c='[email protected]';
        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='[email protected]');
        insert acct;
        oldmap.put(acct.id, acct);
        contact con = new contact();
        con.accountid = acct.id;
        con.LastName = 'TestContact';
        insert con;
        con.Email = '[email protected]';
        update con;        
        newmap.put(con.id, con);           
        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.isInsert || trigger.isUpdate || trigger.isUndelete){
        if(trigger.isDelete || trigger.isUpdate){

dont miss out iconCheck 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) {            
        //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;
            insert listOfOpportunity;
        return listOfOpportunity;
    List<Opportunity> listOfOpportunity= DataFactory.createOpportunity(accountObj.id, 4);
    insert listOfOpportunity;

Test Class Code:-

public class ContcatConnNameOnAccountClass_Test {
    @istest static void CheckAllConcatenateContactWithAccount(){
        List<Account> listOfAccount = new List<Account>();
        Account accountObj = new Account(Name='Test Account 1');
        Account accountConObj = new Account(Name='Test Account 2');
        Account accountCon1Obj = new Account(Name='Test Account 3');
        insert listOfAccount;        
        List<Contact> listOfContact = new List<Contact>();
        Contact conObj= new Contact(LastName='TestContact1', Accountid=accountObj.id);
        Contact con1Obj= new Contact(LastName='TestContact2', Accountid=accountConObj.id);
        Contact con2Obj= new Contact(LastName='TestContact3', Accountid=accountCon1Obj.id);
        insert listOfContact;        
        update conObj;    



Popular Salesforce Blogs