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
Lakshmi SLakshmi S 

Test class code coverage issue ?

Hi Team,

Below code is not covered 75% code coverage, please advise any one how to resolve this issue ?
Handler class
-------------------------
public with sharing class ClientHandlerCtrl {
    
    public static boolean insertRecursiveStop = true;
    public static boolean updateRecursiveStop = true;
    
    public static void ClientInsert(Map<Id,Client__c> newMap){
        
        try{
            Set<String> usrName = new Set<String>();
        
            for(ID clientIds : newMap.keySet()){
                            
                Client__c client = newMap.get(clientIds);
                if(client.Client_Manager__c != Null){
                    usrName.add(client.Client_Manager__c);
                }
            }
            
            List<User> userList = [Select id,Name from User where name in :usrName and IsActive=true];
            
            for(ID clientIds : newMap.keySet()){
                Client__c client = newMap.get(clientIds);
                
                                
                    if(client.Client_Manager__c != Null && userList.size() > 0){
                        
                        for(user usr : userList){
                        
                        if(client.Client_Manager__c == usr.Name){
                            client.Client_Manager_Lookup__c = usr.Id;
                        }
                    }
                                        
                }
                else
                    {
                            client.Client_Manager_Lookup__c = Null;
                    }
                
            }
            
        }
        catch(Exception ex){
            System.debug('Exception caught in client insert method :'+ex.getMessage());
        }
    }
    
    public static void ClientUpdate(Map<Id,Client__c> updateMap){
        
        try{
            
            Set<String> usrName = new Set<String>();
        
            for(ID clientIds : updateMap.keySet()){
                            
                Client__c client = updateMap.get(clientIds);
                if(client.Client_Manager__c != Null){
                    usrName.add(client.Client_Manager__c);
                }
            }
            
            List<User> userList = [Select id,Name from User where name in :usrName and IsActive=true];
            
            for(ID clientIds : updateMap.keySet()){
                Client__c client = updateMap.get(clientIds);
                
                                
                    if(client.Client_Manager__c != Null && userList.size() > 0){
                        
                        for(user usr : userList){
                        
                        if(client.Client_Manager__c == usr.Name){
                            client.Client_Manager_Lookup__c = usr.Id;
                        }
                    }
                                        
                }
                else
                    {
                            client.Client_Manager_Lookup__c = Null;
                    }
                
            }
        }
        catch(Exception ex){
            System.debug('Exception caught in client update method :'+ex.getMessage());
        }
    }
    
}


Test Class
----------------
@isTest
private class TestClientTrigger {
    
    @testSetup
    private static void createTestData(){
        
        List<User> usrList = new List<User>();
        
        for(Integer i=0; i<3 ; i++){
            User usr = new User();
            usr.ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id;
           
            if(i==0){
                usr.FirstName = 'Lakshmi';
                usr.LastName = 'S';
                usr.Email = 'lakshmis@gmail.com';
                usr.Username = 'lakshmis@gmail.com';
                usr.Alias = 'laks';
            }
            else if(i==1){
                usr.FirstName = 'Ajay';
                usr.LastName = 'D';
                usr.Email = 'Ajayd@gmail.com';
                usr.Username = 'Ajayd@gmail.com';
                usr.Alias = 'ajayd';
            }
            else if(i==2){
                usr.FirstName = 'Naresh';
                usr.LastName = 'K';
                usr.Email = 'nareshk@gmail.com;
                usr.Username = 'nareshk@gmail.com';
                usr.Alias = 'naresh';
            }
            
            usr.CompanyName = 'TEST';
            usr.Title = 'title';
            usr.TimeZoneSidKey = 'America/Los_Angeles';
            usr.EmailEncodingKey = 'UTF-8';
            usr.LanguageLocaleKey = 'en_US';
            usr.LocaleSidKey = 'en_US';
            
            usrList.add(usr);
        }
        
        insert usrList;
        
        Account acc = new Account();
        acc.Name = 'Dummy Account';
        insert acc;
        
        Map<Id,Client__c> fsList = new Map<Id,Client__c>();
        
        for(Integer i=0 ; i<3 ; i++){
            Client__c fs = new Client__c();
            fs.Account__c = acc.Id;
            if(i==0){
                fs.Client_Manager__c = 'Lakshmi S';
            }
            else if(i==1){
                fs.Client_Manager__c = 'Ajay D';
            }
            else if(i==2){
                fs.Client_Manager__c = 'Naresh K';
            }
            
            fsList.put(fs.Id,fs);
            
        }
        insert fsList.values();
    }
    
   
    
    @isTest
    private static void updateTest(){
        
        Account acc = [Select id from account];
        
        Client__c fs = [Select id,Client_Manager__c from Client__c limit 1];
        fs.Client_Manager__c = 'Naresh K';
        update fs;
        
    }
    
}
Please let me know.

Thanks,
Lakshmi S

 
Andrew GAndrew G
Well,
there needs to be alot of assumptions/questions to give you an answer.

1. What part of the code is not covered by the test class?

2. If I went to the UI and inserted a Client record or updated a Client record, does the code (class ClientHandlerCtrl)  work as expected?

3. I assume that the class is the handler and it is being invoked by a trigger.  How is the trigger invoking the methods?  How much work is being done there?  I notice that you are passing a Map to the ClientInsert method.  How are you building that Map?  You are aware that newMap is not available in before insert code for trigger?
reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
extract:
newMap
A map of IDs to the new versions of the sObject records.
This map is only available in before update, after insert, after update, and after undelete triggers.


Regards
Andrew
Lakshmi SLakshmi S
Hi Andrew,

Thanks for your reply.
I noticed one issue in my handler class, i am using newMap in before insert trigger that's why insert methond not covered in test class after that i have changed to Trigger.New in before insert trigger, Now it is working fine.
Thanks once again.

Thanks,
Lakshmi S.