You need to sign in to do that
Don't have an account?
salesforce sfdx
Test class:
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 } }
Try this code. It gives 85% code coverage
If this information helps, please mark the answer as best. Thank you