+ Start a Discussion
Jothi EswaranJothi Eswaran 

I am not able to write test class for an apex sharing class. Some one can help ?

This apex sharing class - will provide read only access to previous lead owner and full access to current lead owner when lead ownership is changed. separate update trigger is written 

public class LeadRecordSharingHelper {
    public static void leadRecordShare(Map<Id, Lead> lmap, List<Lead> llist) 
    {  
        Id profileId = userinfo.getProfileId();
        String profileName = [Select Id,Name from Profile where Id=:profileId].Name;
        if (profileName == 'XXX')
        {
                        
            List<LeadShare> leadsharelist = new List<LeadShare>();
            for (Lead ld : llist)
            {
                if (ld.ownerId != lmap.get(ld.Id).ownerId)
                {                               
                    LeadShare lshare = new LeadShare();
                    lshare.LeadAccessLevel = 'Read';
                    lshare.LeadId = ld.Id;
                    lshare.UserOrGroupId = lmap.get(ld.Id).ownerId;
                    lshare.RowCause = Schema.LeadShare.RowCause.Manual;  
                    leadsharelist.add(lshare);
                }
            }     
            insert leadsharelist;
        
        }  
    }
}
Best Answer chosen by Jothi Eswaran
Raj VakatiRaj Vakati
try this code
 
@isTest
public class LeadRecordSharingHelperTest
{
    @isTest
    public static void testLeadInsert ()
    {
      
		Test.startTest();

		
		 Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        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
                        ); 
        
        
        insert uu;
		
		
		Lead objLead = new Lead (LastName = 'Test', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
        insert objLead; 
		
		
	  Lead objLead1 = new Lead (LastName = 'Test data', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
insert objLead1 ;

objLead1.OwnerId = uu.Id ;
update objLead1 ;
	  Test.stopTest();
    }
	
   
}

 

All Answers

Raj VakatiRaj Vakati
try this code
 
@isTest
public class LeadRecordSharingHelperTest
{
    @isTest
    public static void testLeadInsert ()
    {
      
		Test.startTest();

		
		 Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        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
                        ); 
        
        
        insert uu;
		
		
		Lead objLead = new Lead (LastName = 'Test', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
        insert objLead; 
		
		
	  Lead objLead1 = new Lead (LastName = 'Test data', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
insert objLead1 ;

objLead1.OwnerId = uu.Id ;
update objLead1 ;
	  Test.stopTest();
    }
	
   
}

 
This was selected as the best answer
Jothi EswaranJothi Eswaran
Thank you much Raj for your time. 
Raj VakatiRaj Vakati
close this thread