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
Vani KumariVani Kumari 

Need assistance with Lead share test class

Hello Folks

I am new to the apex environment, i am trying to write a test class for the below lead sharing class, my trigger and handler class works perfectly fine,  but i am not able to cover the class itself, some assistance would be very helpfull

My Trigger
 
trigger LeadTrigger on Lead (before insert,before update,after insert,after update){ 
IF(Trigger.isAfter && Trigger.isupdate){
        LeadHelper.leadShareInsertUpdate(Trigger.New);

}
}

My Helper class
 
public class LeadHelper {
    public static List<LeadShare> csShareList = new List<LeadShare>();
    public static List<LeadShare> removeShareList = new List<LeadShare>();
    
    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;
                if(cs.OwnerId!= cs.CRM_User__c){
                    csShareList.add(csShare);
                }
            }
            
            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;
        }
    
    }
}

My Test Class
 
@istest
public class LeadTriggerTest {
        public static testMethod void MyTestMethod_To_Share_Leads() {
        Set<ID> newidsSet1 = new Set<ID>();
        Set<ID> newidsSet2 = new Set<ID>();
        List<Lead> NewLeadsList1 = new List<Lead>();
        List<Lead> NewLeadsList2 = new List<Lead>();
       Profile pf= [Select Id from profile Where Name ='Standard User']; 
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-',''); 
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id, 
                         ManagerId= UserInfo.getUserId());
        System.runAs(uu){
            //Inserting the Leads with CRM_User__c
            for (Integer i=0;i<10;i++){
                NewLeadsList1.add(new Lead(FirstName='First',LastName='Name '+i,
                                           Email='email'+i+'@gmail.com',Company='ABSYZ',CRM_User__c=uu.Id));
            }
            insert NewLeadsList1;
            
            //get a set of all new created Lead ids
            for (Lead l : NewLeadsList1){
                newidsSet1.add(l.id);
            }
            
            //assert that 10 shares were created
            List<LeadShare> shares = [select id, Lead.FirstName from LeadShare where LeadId IN :newidsSet1 and RowCause = 'Manual' AND Lead.FirstName Like '%First%'];
            System.assertEquals(shares.size(),10);
            
            
            //Inserting the Leads with CRM_User__c is null
            for (Integer i=0;i<10;i++){
                NewLeadsList2.add(new Lead(FirstName='Name', LastName='LeadTesting'+i+orgId,
                                           Email='test'+i+'@gmail.com', Company='XYZ'));
            }
            insert NewLeadsList2;
            
            //get a set of all new created Lead ids
            for (Lead l : NewLeadsList1){
                newidsSet2.add(l.id);
            }
            //assert that 10 shares were created
            List<LeadShare> sharesList = [select id, Lead.FirstName from LeadShare where LeadId IN :newidsSet2 and RowCause = 'Manual' AND Lead.FirstName LIKE '%Name%'];
            System.assertEquals(sharesList.size(),0);
        }
    }
    

}

 
SwethaSwetha (Salesforce Developers) 
HI Vani,

The code provided in question does not highlight any uncovered lines. Since this code requires an understanding of your implementation, it might not be possible to provide exact edit suggestions. However, the below articles give a good insight into how coverage can be improved to get started

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 

Examples of LeadShare test class:
https://salesforce.stackexchange.com/questions/79583/creating-test-code-for-an-after-insert-trigger

https://salesforce.stackexchange.com/questions/33859/inactive-owner-or-user-test-class

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you