function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
salesforce sfdxsalesforce sfdx 

Hi folks Can any one help me in writing the test class for the below program

public class LeadTriggerHandler {
 //This method is called before Inserting.
 public void BeforeInsertMethod(List<Lead> LeadsRec){
       List<Lead> Leadlist = New List<Lead>();  
        //collection of the data in trigger.new
    for(Lead ld:LeadsRec){
        
        if (!ld.IsConverted){ //checking it was converted or Not
        If(ld.Referral__c != null){
           List<Account> Accountlist =[select Id,name,BillingPostalCode from Account where Id=:ld.Referral__c];
            
            If(!Accountlist.isEmpty()){
         If(Accountlist[0].BillingPostalCode != null){
                     List<Zip_Code__c> Zipcodlist = [Select Territory__c from Zip_Code__c where Name =:Accountlist[0].BillingPostalCode];
                     List<Territory__c> Territory2  =[Select id, Name from Territory__c where Name ='Whitespace'];
                    If(!Zipcodlist.isEmpty()){
               
                    ld.Territory__c = Zipcodlist[0].Territory__c;
                     
                    }else{
                     If(!Territory2.isEmpty()){
                 ld.Territory__c = Territory2[0].id;
                     }
                }
                }else If(Accountlist[0].BillingPostalCode == '' || Accountlist[0].BillingPostalCode == Null){
                    system.debug('Entered in account postal code empty loop');
         List<Zip_Code__c> Zipcodlist2 = [Select Territory__c from Zip_Code__c where Name =:ld.postalcode];
         List<Territory__c> Territory2  =[Select id, Name from Territory__c where Name ='Empty'];  
            If(!Zipcodlist2.isEmpty()){
                
                    ld.Territory__c = Zipcodlist2[0].Territory__c;
                     
                 }else{ 
                     If(!Territory2.isEmpty()){
                 ld.Territory__c = Territory2[0].id;
                     }
            } 
                }
             }    
        }
            
        else If(ld.Referral__c == null && ld.postalcode != null){ 
                  
         //Fetching the Territory name from Lead PostalCode
         List<Zip_Code__c> Zipcodlist2 = [Select Territory__c from Zip_Code__c where Name =:ld.postalcode];
         
           List<Territory__c> Territory2  =[Select id, Name from Territory__c where Name ='Empty'];
            
            If(!Zipcodlist2.isEmpty()){
                
                    ld.Territory__c = Zipcodlist2[0].Territory__c;
                        
            }else{
                     If(!Territory2.isEmpty()){
                 ld.Territory__c = Territory2[0].id;
                     }
            }
            
        }
      
    }
    }
   
    }
}

Test class:
@isTest
private class LeadTriggerHandlerTest {

    
    static testMethod void BeforeInsertMethod(){
        List<Lead> lstLead =   new List<Lead>{
                          new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open'),
                          new Lead(Company = 'Nike', LastName = 'John', Status = 'Open'),
                          new Lead(Company = 'Miles', LastName = 'Davis', Status = 'Open'),
                          new Lead(Company = 'Reebok', LastName = 'Hillen', Status = 'Open'),
                          new Lead(Company = 'Addidas', LastName = 'Shrin', Status = 'Open')
                         }; 
        
                             
        LeadTriggerHandler     H= new LeadTriggerHandler ();
        H.BeforeInsertMethod(lstLead);

     //need to cover the codecoverage
    }
    
    
    
}

 
SwethaSwetha (Salesforce Developers) 
HI ,

Try this code. It gives 85% code coverage
@istest
public class LeadTriggerHandlerTest {
    @isTest static void leadtest() {
        Account acc= new Account();
        acc.name=‘sample’;
        acc.BillingPostalCode=‘12345’;
        insert acc;
Account acc1= new Account();
        acc1.name=‘sample’;
        insert acc1;
        Territory__c ts= new Territory__c();
        ts.name=‘Whitespace’;
        insert ts;
         Territory__c ts1= new Territory__c();
        ts1.name=‘Empty’;
        insert ts1;
        Zip_Code__c zs= new Zip_Code__c();
        zs.name=‘12345’;
        zs.Territory__c= ts.id;
        insert zs;
     Lead ls= new Lead();
        ls.Company=‘sample company’;
        ls.LastName=‘sample lead’;
        ls.Status=‘Qualified’;
        ls.Referral__c=acc.id;
        insert ls;
          Lead ls1= new Lead();
        ls1.Company=‘sample company’;
        ls1.LastName=‘sample lead’;
        ls1.Status=‘Qualified’;
        ls1.Referral__c=acc1.id;
        ls1.PostalCode=‘12323’;
        insert ls1;
                  Lead ls2= new Lead();
        ls2.Company=‘sample company’;
        ls2.LastName=‘sample lead’;
        ls2.Status=‘Qualified’;
        ls2.PostalCode=‘12323’;
        insert ls2;
    }
}

If this information helps, please mark the answer as best. Thank you