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
Janaki RaoJanaki Rao 

How do to make the code coverage to 100% for the below Test Class

Hello Developers

I am Working on a Test class where it has already covered 96%, how do i make it to 100%, I have Tried all the Best Pratices for the Test class, still i am not able to make it to 100%

This is my Helper Class

public class LeadHelper {
    public static List<LeadShare> csShareList = new List<LeadShare>();
    public static List<LeadShare> removeShareList = new List<LeadShare>();
    public static List<Lead> leadsToAddAccountId = new List<Lead>();   
    public static List<String> leadCompanyNames = new List<String>();
    public static Map<String, Id> matchingAcctNames = new Map<String,Id>();
    
    public static void leadShareInsertUpdate(List<Lead> leads){
        csShareList = new List<LeadShare>();
        removeShareList = new List<LeadShare>();
        Set<Id> ldIds = new Set<Id>();
        for( Lead cs : leads) {
            if( cs.CRM_User__c != NULL ) {
                // Create a new LeadShare object for each Lead where CRM_User__c field is not NULL.
                LeadShare csShare = new LeadShare();
                // Give Read write access to that user for this particular Lead record.
                csShare.LeadAccessLevel = 'edit';
                // Assign Lead Id of Lead record.
                csShare.LeadId = cs.id;
                // Assign user id to grant read write access to this particular Lead record.
                csShare.UserOrGroupId = cs.CRM_User__c;
                csShareList.add( csShare );
            }
            if( cs.CRM_User__c == NULL ) {
                ldIds.add( cs.id);
            }
        }
        
        if(ldIds.size()>0){
            removeShareList = [Select id,LeadId,UserOrGroupId from LeadShare where LeadId=:ldIds AND RowCause = 'Manual'];
            if(removeShareList.size()>0){
                delete removeShareList;
            }
        }
        if( csShareList != null && csShareList.size() != 0 ) {
            insert csShareList;
            update csShareList;
        }
    }
    
    public static void leadsToAddAccountId(List<Lead> leads){
        leadsToAddAccountId = new List<Lead>();   
        leadCompanyNames = new List<String>();
        matchingAcctNames = new Map<String,Id>();
        
        for (Lead newLead : leads){
            leadsToAddAccountId.add(newLead);
            leadCompanyNames.add(newLead.Company);
        }
        for(Account acct : [Select Id, Name from Account where Name =: leadCompanyNames]){
            matchingAcctNames.put(acct.Name, acct.Id);
        }
        
        for(Lead myLead : leadsToAddAccountId ){
            myLead.Account__c = matchingAcctNames.get(myLead.Company);
        }
    }
    
    public static void leadIncrementroundrobin(List<Lead> leads){
        
        Id recordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
        List<Lead> leadList = [Select Id, Updated_Region__c, RecordTypeId From Lead where RecordTypeId=:recordTypeId  ALL ROWS];
        
        Map<String, List<Lead>> countryLeadsMap = new Map<String, List<Lead>>();
        
        for (Lead l : leadList) {
            if (!countryLeadsMap.containsKey(l.Updated_Region__c)) {
                countryLeadsMap.put(l.Updated_Region__c, new List<Lead>());   
            }
            countryLeadsMap.get(l.Updated_Region__c).add(l);
        }
        
        for (Lead nl : leads){
            if(nl.RecordTypeId == recordTypeId){
                if (!countryLeadsMap.containsKey(nl.Updated_Region__c)) {
                    countryLeadsMap.put(nl.Updated_Region__c, new List<Lead>());
                    
                }
                countryLeadsMap.get(nl.Updated_Region__c).add(nl);
                if (nl.Updated_Region__c == 'India') {
                    nl.Lead_No_Region_IN__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
                
                if (nl.Updated_Region__c == 'US') {
                    nl.Lead_No_Region_USA__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
                
                if (nl.Updated_Region__c == 'EU') {
                    nl.Lead_No_Region_EU__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
                
                if (nl.Updated_Region__c == 'ME') {
                    nl.Lead_No_Region_ME__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
                if (nl.Updated_Region__c == 'SEA1') {
                    nl.Lead_No_Region_SEA1__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
                if (nl.Updated_Region__c == 'SEA2') {
                    nl.Lead_No_Region_SEA2__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
            }
        }
    }
}

-----------------------------------

My test class

@isTest(seealldata=true)
private class LeadTrigger_Test {
    
    // test that newly inserted records marked as pubic=true have corresponding shares created
    public static testMethod void testAddShares() {
        
        Set<ID> ids = new Set<ID>();
        List<Lead> Leads = new List<Lead>();
        Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
        User u = [Select id,name from user where isactive=true Limit 1];
        for (Integer i=0;i<10;i++)
            Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
                               Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
        
        insert Leads;
        
        // get a set of all new created ids
        for (Lead c : Leads)
            ids.add(c.id);
        
        // assert that 50 shares were created
        List<LeadShare> shares = [select id from LeadShare where 
                                  LeadId IN :ids and RowCause = 'Manual'];
        
    }
    
    // insert records and switch them from public = true to public = false
    public static testMethod void testUpdateContacts() {
        
        Set<ID> ids = new Set<ID>();
        List<Lead> Leads = new List<Lead>();
        Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
        User u = [Select id,name from user where isactive=true Limit 1];
        
        for (Integer i=0;i<50;i++)
            Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
                               Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
        
        insert Leads;
        
        for (Lead c : Leads)
            ids.add(c.id);
        
        update Leads;
        
        // assert that 0 shares exist
        List<LeadShare> shares = [select id from LeadShare where 
                                  LeadId IN :ids and RowCause = 'Manual'];
        System.assertNotEquals(shares.size(),0);
        
        for (Lead c : Leads)
            c.CRM_User__c=u.Id;
        
        update Leads;
        
        // assert that 50 shares were created
        shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
        System.assertEquals(shares.size(),50);
        
        for (Lead c : Leads)
            c.CRM_User__c=u.Id;
        
        update Leads;
        
        // assert that 0 shares exist
        shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
        System.assertNotEquals(shares.size(),0);
        
    }
    public static testMethod void validateLeadAndAccount() {
        List<Lead> leadList = new List<Lead>();
        Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
        Account acc = new Account();
        Acc.Name = 'Test11'; 
        insert acc;
        
        Lead l1 = new Lead ();
        l1.LastName = 'Test';
        l1.RecordTypeId = RecordTypeIdLead;
        l1.Company = 'Test11'; 
        l1.Country = 'India';
        l1.Lead_Category__c = 'Churned Customer';
        l1.Status ='Enquiry'; 
        leadList.add(l1);
        
        Lead l2 = new Lead();
        l2.Country = 'USA';
        l2.Lead_Category__c = 'Churned Customer';
        l2.Company = 'Test21';
        l2.RecordTypeId = RecordTypeIdLead;
        l2.LastName = 'testLast21';
        l2.Status = 'Enquiry';
        leadList.add(l2);
        
        Lead l3 = new Lead();
        l3.Country = 'Taiwan';
        l3.Lead_Category__c = 'Churned Customer';
        l3.Company = 'Test21';
        l3.RecordTypeId = RecordTypeIdLead;
        l3.LastName = 'testLast21';
        l3.Status = 'Enquiry';
        leadList.add(l3);
        
        Lead l4 = new Lead();
        l4.Country = 'Egypt';
        l4.Lead_Category__c = 'Churned Customer';
        l4.Company = 'Test21';
        l4.RecordTypeId = RecordTypeIdLead;
        l4.LastName = 'testLast21';
        l4.Status = 'Enquiry';
        leadList.add(l4);
        
        Lead l5 = new Lead();
        l5.Country = 'Germany';
        l5.Lead_Category__c = 'Churned Customer';
        l5.Company = 'Test21';
        l5.RecordTypeId = RecordTypeIdLead;
        l5.LastName = 'testLast21';
        l5.Status = 'Enquiry';
        leadList.add(l5);
        
        Lead l6 = new Lead();
        l6.Country = 'Malaysia';
        l6.Lead_Category__c = 'Churned Customer';
        l6.Company = 'Test21';
        l6.RecordTypeId = RecordTypeIdLead;
        l6.LastName = 'testLast21';
        l6.Status = 'Enquiry';
        leadList.add(l6);
        
        Lead l7 = new Lead();
        l7.Country = 'Singapore';
        l7.Lead_Category__c = 'Churned Customer';
        l7.Company = 'Test21';
        l7.RecordTypeId = RecordTypeIdLead;
        l7.LastName = 'testLast21';
        l7.Status = 'Enquiry';
        leadList.add(l7);
        
        Test.startTest();
        
        Insert leadList;
        
        Test.stopTest();
        
    }
}


This is where the code is not getting covered

This is an 2nd Line which are not getting covered
Janaki RaoJanaki Rao
My Revised Apex Class

public class LeadHelper {
    public static List<LeadShare> csShareList = new List<LeadShare>();
    public static List<LeadShare> removeShareList = new List<LeadShare>();
    public static List<Lead> leadsToAddAccountId = new List<Lead>();   
    public static List<String> leadCompanyNames = new List<String>();
    public static Map<String, Id> matchingAcctNames = new Map<String,Id>();
    
    public static void leadShareInsertUpdate(List<Lead> leads){
        csShareList = new List<LeadShare>();
        removeShareList = new List<LeadShare>();
        Set<Id> ldIds = new Set<Id>();
        for( Lead cs : leads) {
            if( cs.CRM_User__c != NULL ) {
                // Create a new LeadShare object for each Lead where CRM_User__c field is not NULL.
                LeadShare csShare = new LeadShare();
                // Give Read write access to that user for this particular Lead record.
                csShare.LeadAccessLevel = 'edit';
                // Assign Lead Id of Lead record.
                csShare.LeadId = cs.id;
                // Assign user id to grant read write access to this particular Lead record.
                csShare.UserOrGroupId = cs.CRM_User__c;
                csShareList.add( csShare );
            }
            if( cs.CRM_User__c == NULL ) {
                ldIds.add( cs.id);
            }
        }
        
        if(ldIds.size()>0){
            removeShareList = [Select id,LeadId,UserOrGroupId from LeadShare where LeadId=:ldIds AND RowCause = 'Manual'];
            if(removeShareList.size()>0){
                delete removeShareList;
            }
        }
        if( csShareList != null && csShareList.size() != 0 ) {
            insert csShareList;
            update csShareList;
        }
    }
    
    public static void leadsToAddAccountId(List<Lead> leads){
        leadsToAddAccountId = new List<Lead>();   
        leadCompanyNames = new List<String>();
        matchingAcctNames = new Map<String,Id>();
        
        for (Lead newLead : leads){
            leadsToAddAccountId.add(newLead);
            leadCompanyNames.add(newLead.Company);
        }
        for(Account acct : [Select Id, Name from Account where Name =: leadCompanyNames]){
            matchingAcctNames.put(acct.Name, acct.Id);
        }
        
        for(Lead myLead : leadsToAddAccountId ){
            myLead.Account__c = matchingAcctNames.get(myLead.Company);
        }
    }
    
    public static void leadIncrementroundrobin(List<Lead> leads){
        
        Id recordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
        List<Lead> leadList = [Select Id, Updated_Region__c, RecordTypeId From Lead where RecordTypeId=:recordTypeId  ALL ROWS];
        
        Map<String, List<Lead>> countryLeadsMap = new Map<String, List<Lead>>();
        
        for (Lead l : leadList) {
            if (!countryLeadsMap.containsKey(l.Updated_Region__c)) {
                countryLeadsMap.put(l.Updated_Region__c, new List<Lead>());   
            }
            countryLeadsMap.get(l.Updated_Region__c).add(l);
        }
        
        for (Lead nl : leads){
            if(nl.RecordTypeId == recordTypeId){
                if (!countryLeadsMap.containsKey(nl.Updated_Region__c)) {
                    countryLeadsMap.put(nl.Updated_Region__c, new List<Lead>());
                    
                }
                countryLeadsMap.get(nl.Updated_Region__c).add(nl);
                if (nl.Updated_Region__c == 'India') {
                    nl.Lead_No_Region_IN__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
                
                if (nl.Updated_Region__c == 'US') {
                    nl.Lead_No_Region_USA__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
                
                if (nl.Updated_Region__c == 'EU') {
                    nl.Lead_No_Region_EU__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
                
                if (nl.Updated_Region__c == 'ME') {
                    nl.Lead_No_Region_ME__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
                if (nl.Updated_Region__c == 'SEA1') {
                    nl.Lead_No_Region_SEA1__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
                if (nl.Updated_Region__c == 'SEA2') {
                    nl.Lead_No_Region_SEA2__c = countryLeadsMap.get(nl.Updated_Region__c).size();
                }
            }
        }
    }
}


 
My revised Test Class

@isTest(seealldata=true)
private class LeadTrigger_Test {
    
    // test that newly inserted records marked as pubic=true have corresponding shares created
    public static testMethod void testAddShares() {
        
        Set<ID> ids = new Set<ID>();
        List<Lead> Leads = new List<Lead>();
        Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
        User u = [Select id,name from user where isactive=true Limit 1];
        for (Integer i=0;i<10;i++)
            Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
                               Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
        
        insert Leads;
        
        // get a set of all new created ids
        for (Lead c : Leads)
            ids.add(c.id);
        
        // assert that 50 shares were created
        List<LeadShare> shares = [select id from LeadShare where 
                                  LeadId IN :ids and RowCause = 'Manual'];
        
    }
    
    // insert records and switch them from public = true to public = false
    public static testMethod void testUpdateContacts() {
        
        Set<ID> ids = new Set<ID>();
        List<Lead> Leads = new List<Lead>();
        Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
        User u = [Select id,name from user where isactive=true Limit 1];
        
        for (Integer i=0;i<50;i++)
            Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
                               Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
        
        insert Leads;
        
        for (Lead c : Leads)
            ids.add(c.id);
        
        update Leads;
        
        // assert that 0 shares exist
        List<LeadShare> shares = [select id from LeadShare where 
                                  LeadId IN :ids and RowCause = 'Manual'];
        System.assertNotEquals(shares.size(),0);
        
        for (Lead c : Leads)
            c.CRM_User__c=u.Id;
        
        update Leads;
        
        // assert that 50 shares were created
        shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
        System.assertEquals(shares.size(),50);
        
        for (Lead c : Leads)
            c.CRM_User__c=u.Id;
        
        update Leads;
        
        // assert that 0 shares exist
        shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
        System.assertNotEquals(shares.size(),0);
        
    }
    public static testMethod void validateLeadAndAccount() {
        
        List<Lead> leadList = new List<Lead>();
        Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
        Account acc = new Account();
        Acc.Name = 'Test11'; 
        insert acc;
        
        test.startTest();
        
        Lead l1 = new Lead ();
        l1.LastName = 'Test';
        l1.RecordTypeId = RecordTypeIdLead;
        l1.Company = 'Test11'; 
        l1.Country = 'India';
        l1.Lead_Category__c = 'Churned Customer';
        l1.Status ='Enquiry'; 
        leadList.add(l1);
        
        Lead l2 = new Lead();
        l2.Country = 'USA';
        l2.Lead_Category__c = 'Churned Customer';
        l2.Company = 'Test21';
        l2.RecordTypeId = RecordTypeIdLead;
        l2.LastName = 'testLast21';
        l2.Status = 'Enquiry';
        leadList.add(l2);
        
        Lead l3 = new Lead();
        l3.Country = 'Taiwan';
        l3.Lead_Category__c = 'Churned Customer';
        l3.Company = 'Test21';
        l3.RecordTypeId = RecordTypeIdLead;
        l3.LastName = 'testLast21';
        l3.Status = 'Enquiry';
        leadList.add(l3);
        
        Lead l4 = new Lead();
        l4.Country = 'Egypt';
        l4.Lead_Category__c = 'Churned Customer';
        l4.Company = 'Test21';
        l4.RecordTypeId = RecordTypeIdLead;
        l4.LastName = 'testLast21';
        l4.Status = 'Enquiry';
        leadList.add(l4);
        
        Lead l5 = new Lead();
        l5.Country = 'Germany';
        l5.Lead_Category__c = 'Churned Customer';
        l5.Company = 'Test21';
        l5.RecordTypeId = RecordTypeIdLead;
        l5.LastName = 'testLast21';
        l5.Status = 'Enquiry';
        leadList.add(l5);
        
        Lead l6 = new Lead();
        l6.Country = 'Malaysia';
        l6.Lead_Category__c = 'Churned Customer';
        l6.Company = 'Test21';
        l6.RecordTypeId = RecordTypeIdLead;
        l6.LastName = 'testLast21';
        l6.Status = 'Enquiry';
        leadList.add(l6);
        
        Lead l7 = new Lead();
        l7.Country = 'Singapore';
        l7.Lead_Category__c = 'Churned Customer';
        l7.Company = 'Test21';
        l7.RecordTypeId = RecordTypeIdLead;
        l7.LastName = 'testLast21';
        l7.Status = 'Enquiry';
        leadList.add(l7);
        
        Insert leadList;
        
        test.stoptest();
        
    }
}