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
PhoenixRising12PhoenixRising12 

Test Class is only at 71% Coverage

I have a Trigger and Helper Class that it calls which merges new leads into existing accounts if the email, name or phone matches an existing account. 

I'm trying to create a test class for the helper class but it's only at 70% coverage. What can I change about this testclass to have it achieve 100% coverage? 

Handler Class
public class DuplicateMergeTriggerHandler {
    
    public static void MergeHandler(Set<Id> leadIds) {
	
        List<String> LeadListEmails = new List<String>();
        List<String> LeadNames = new List<String>();
        List<String> LeadPhones = new List<String>();
        
        List<Lead> myleads = [SELECT Id, Name, Email, Phone FROM Lead WHERE ID IN :leadIds];
        
        for (Lead L1 : myleads) {
           LeadListEmails.add(L1.Email);
        }
		
        for (Lead L2 : myleads){
            LeadNames.add(L2.Name);
        }
        
        for (Lead L3 : myleads){
            
            LeadPhones.add(L3.Phone);
        }

        Map<String,Account> mapAccountsEmail = new Map<String,Account>();
         
        for (Account A : [SELECT Id, PersonEmail FROM Account WHERE PersonEmail IN :LeadListEmails]){    
            mapAccountsEmail.put(A.PersonEmail, A);   
        }
        
        Map<String,Account> mapAccountsName = new Map<String,Account>();
        
        for (Account A1 : [SELECT Id, Name FROM Account WHERE Name IN :LeadNames]){
            
           mapAccountsName.put(A1.Name, A1);
        }
        
       Map<String,Account> mapAccountsPhone = new Map<String,Account>();
        for (Account A2 : [SELECT Id, Phone FROM Account WHERE Phone IN :LeadPhones]){
            
            mapAccountsPhone.put(A2.Phone, A2);
        }
        
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM Leadstatus WHERE IsConverted = TRUE LIMIT 1];
        
        List<Database.LeadConvert> lstLC = new List<Database.LeadConvert>();
	  
        for (lead MyLead : myleads) {
            
 	        Account matchingAccountEmail = mapAccountsEmail.get(MyLead.Email);
            Account matchingAccountName = mapAccountsName.get(MyLead.Name);
            Account matchingAccountPhone = mapAccountsPhone.get(MyLead.Phone);
	 
            if(matchingAccountEmail != NULL){
         		Database.LeadConvert lc = new database.LeadConvert();
 				lc.setLeadId(MyLead.Id);
                lc.setDoNotCreateOpportunity(TRUE);
         		lc.setAccountId(matchingAccountEmail.Id);
                lc.setConvertedStatus(convertStatus.MasterLabel);
                lstLc.add(lc);
            } else if (matchingAccountName != NULL){
                Database.LeadConvert lc1 = new database.LeadConvert();
 				lc1.setLeadId(MyLead.Id);
                lc1.setDoNotCreateOpportunity(TRUE);
         		lc1.setAccountId(matchingAccountName.Id);
                lc1.setConvertedStatus(convertStatus.MasterLabel);
                lstLc.add(lc1);
            } else if (matchingAccountPhone != NULL){
                
                Database.LeadConvert lc2 = new database.LeadConvert();
 				lc2.setLeadId(MyLead.Id);
                lc2.setDoNotCreateOpportunity(TRUE);
         		lc2.setAccountId(matchingAccountPhone.Id);
                lc2.setConvertedStatus(convertStatus.MasterLabel);
                lstLc.add(lc2);
                
            }        
        }
            
        if(!lstLc.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(lstLc);
            System.debug(+'result'+lcr); 
            
        } 
            
    }}
Trigger
trigger DuplicateMergeTrigger on Lead (after Insert) {
    
    Set<ID> leadIds= new Set<ID>();
    
	for(Lead myLead : Trigger.New) {
        if(myLead.IsConverted == FALSE && myLead.Status == 'New' && myLead.College_Corps__c == TRUE){
            leadIds.add(myLead.Id);
        }
    } 

    if(leadIds.size() > 0 ) {
        DuplicateMergeTriggerHandler.MergeHandler(leadIds);
    }
}

Test Class
@istest
public class DuplicateMergeTriggerHandlerTest{
    
    static testmethod void testleadmerging(){
        
        Lead objLead = new Lead();
        objLead.LastName = 'Kim';
        objLead.FirstName = 'Koi';
        objLead.College_Corps__c = TRUE;
        objLead.Status = 'New';
        insert objLead;

       	Lead Lead2 = new Lead();
        Lead2.LastName = 'Test';
        Lead2.Email = 'salesforce12@gmail.com';
        Lead2.College_Corps__c = TRUE;
        Lead2.Status = 'New';
        insert Lead2;
        
        Lead Lead3 = new Lead();
        Lead3.LastName = 'Kim1';
        Lead3.Phone = '9125603000';
        Lead3.College_Corps__c = TRUE;
        Lead3.Status = 'New';
        insert Lead3;
        
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM Leadstatus WHERE IsConverted = TRUE LIMIT 1];
       
        Account acc1 = new Account();
        acc1.LastName = 'Kim';
        acc1.FirstName = 'Koi';
        insert acc1;
        
        Account acc2 = new Account();
        acc2.LastName = 'Kimbo';
        acc2.PersonEmail = 'salesforce12@gmail.com';
        insert acc2;
        
        Account acc3 = new Account();
        acc3.LastName = 'Test Test';
        acc3.Phone = '9125603000';
        insert acc3;
        
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(objLead.Id);
        lc.setDoNotCreateOpportunity(TRUE);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        lc.setAccountId(acc1.Id);
        
        Database.LeadConvertResult lcr = Database.convertLead(lc, false);
        
        System.assert(lcr.isSuccess());
 
        Database.LeadConvert lc1 = new database.LeadConvert();
        lc1.setLeadId(Lead2.Id);
        lc1.setDoNotCreateOpportunity(TRUE);
        lc1.setConvertedStatus(convertStatus.MasterLabel);
        lc1.setAccountId(acc2.Id);
        
        Database.LeadConvertResult lcr2 = Database.convertLead(lc1, false);
        
        System.assert(lcr2.isSuccess());
        
        Database.LeadConvert lc2 = new database.LeadConvert();
        lc2.setLeadId(Lead3.Id);
        lc2.setDoNotCreateOpportunity(TRUE);
        lc2.setConvertedStatus(convertStatus.MasterLabel);
        lc2.setAccountId(acc3.Id);
        
        Database.LeadConvertResult lcr3 = Database.convertLead(lc2, false);
        
        System.assert(lcr3.isSuccess());
        

        
    }    
 

}

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi ,

Can you highlight the part which is not getting covered so experts can suggest you based on that.

Thanks,