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
sumit dsumit d 

how to get Test coverage for test class

Hi All,
          I have a trigger helper class give below:-
public without sharing class SessionTriggerHelper {
   
    public static List<Session__c> newSession = new List<Session__c>();
    public static List<Session__c> oldSession = new List<Session__c>(); 
    public static Map<Id, Session__c> newMapSession =new Map<Id, Session__c>();
    public static Map<Id, Session__c> oldMapSession =new Map<Id, Session__c>();
    
    public static boolean runTrigger = TRUE;
    
     
    public static void createWebinarAndMeeting(){
        for(Session__c ss : newSession){
            if(ss.rie__Zoom_URL__c == Null && ss.rie__Session_Status__c == 'Ready to Publish'){
                String zoomUserId = '';
                
                if(ss.rie__ZoomUserId__c != Null && ss.rie__Webinar_Creation_Allowed__c){
                   zoomUserId = ss.rie__ZoomUserId__c; 
                }else{
                   zoomUserId = Label.Zoom_User_Id; 
                }
                
                if(ss.rie__Session_Type__c == 'Meeting'){
                   createMeeting(ss.rie__ZoomUserId__c, ss); 
                }else{
                   createWebinar(zoomUserId, ss); 
                }
                
                
            }
        }
    }
    
    
    
    public static void createWebinar(String zoomUserId, Session__c ss){
        Map<String, Object> reqMap = new Map<String, Object>();
        Map<String, Object> recurrenceMap = new Map<String, Object>();
        recurrenceMap.put('type' , 1);
        recurrenceMap.put('repeat_interval' , 1);
        recurrenceMap.put('end_date_time' , ss.rie__Session_End_Time__c);
        
        Map<String, Object> settingMap = new Map<String, Object>();
        settingMap.put('host_video' , 'true');
        settingMap.put('panelists_video' , 'true');
        settingMap.put('practice_session' , 'true');
        settingMap.put('hd_video' , 'true');
        settingMap.put('approval_type' , 0);
        settingMap.put('registration_type' , 2);
        settingMap.put('audio' , 'both');
        settingMap.put('auto_recording' , 'none');
        settingMap.put('enforce_login' , 'false');
        settingMap.put('close_registration' , 'true');
        settingMap.put('show_share_button' , 'true');
        settingMap.put('allow_multiple_devices' , 'false');
        settingMap.put('registrants_email_notification' , 'true');
        
        reqMap.put('topic', ss.Name);
        reqMap.put('type', 5);
        DateTime startTime = ss.rie__Date_Time__c.addHours(-7);
        Map<String, String> timeZoneMap = getDifferentTimeZones(ss.rie__Event_Time_Zone__c);

        Integer timeZoneHour = Integer.valueOf(timeZoneMap.get('timeZoneHour'));
        Integer timeZoneMinute = Integer.valueOf(timeZoneMap.get('timeZoneMinute'));
        reqMap.put('start_time', startTime.addHours(timeZoneHour).addMinutes(timeZoneMinute));
        if(ss.rie__Date_Time__c != Null && ss.rie__Session_End_Time__c != Null){
            Double st = ss.rie__Session_End_Time__c.getTime() - ss.rie__Date_Time__c.getTime();
            st = st/(1000*60);
            reqMap.put('duration', Integer.valueOf(st));
        }
        
        reqMap.put('timezone', timeZoneMap.get('timezoneName'));
        if(ss.rie__Meeting_Password__c != Null){
            reqMap.put('password', ss.rie__Meeting_Password__c);
        }else{
            Integer len = 10;
            Blob blobkey = crypto.generateAesKey(128);
            String key = EncodingUtil.convertToHex(blobkey);
            String pwd = key.substring(0,len);
            reqMap.put('password', pwd);
        }
        reqMap.put('agenda', ss.Name);
        reqMap.put('recurrence',recurrenceMap);
        reqMap.put('settings', settingMap);
        String  reqBody = JSON.serialize(reqMap);
        
        ZoomService.createWebinar(reqBody, zoomUserId, ss.Id);
    }
    
    public static void createMeeting(String zoomUserId, Session__c ss){
        Map<String, Object> reqMap = new Map<String, Object>();
        Map<String, Object> recurrenceMap = new Map<String, Object>();
        recurrenceMap.put('type' , 3);
        recurrenceMap.put('repeat_interval' , 1);
        recurrenceMap.put('weekly_days' , '4');
        recurrenceMap.put('monthly_day' , 10);
        recurrenceMap.put('monthly_week' , 2);
        recurrenceMap.put('end_date_time' , ss.rie__Session_End_Time__c);
        
        Map<String, Object> settingMap = new Map<String, Object>();
        settingMap.put('host_video' , false);
        settingMap.put('participant_video' , false);
        settingMap.put('cn_meeting' , false);
        settingMap.put('in_meeting' , false);
        settingMap.put('join_before_host' , true);
        settingMap.put('mute_upon_entry' , false);
        settingMap.put('watermark' , false);
        settingMap.put('waiting_room' , false);
        settingMap.put('use_pmi' , false);
        settingMap.put('approval_type' , 0);
        settingMap.put('registration_type' , 2);
        settingMap.put('audio' , 'both');
        settingMap.put('auto_recording' , 'none');
        
        reqMap.put('topic', ss.Name);
        reqMap.put('type', 3);
        DateTime startTime = ss.rie__Date_Time__c.addHours(-7);
        Map<String, String> timeZoneMap = getDifferentTimeZones(ss.rie__Event_Time_Zone__c);
        Integer timeZoneHour = Integer.valueOf(timeZoneMap.get('timeZoneHour'));
        Integer timeZoneMinute = Integer.valueOf(timeZoneMap.get('timeZoneMinute'));
        reqMap.put('start_time', startTime.addHours(timeZoneHour).addMinutes(timeZoneMinute));
        if(ss.rie__Date_Time__c != Null && ss.rie__Session_End_Time__c != Null){
            Double st = ss.rie__Session_End_Time__c.getTime() - ss.rie__Date_Time__c.getTime();
            st = st/(1000*60);
            reqMap.put('duration', Integer.valueOf(st));
        }
        
        reqMap.put('timezone', timeZoneMap.get('timezoneName'));
        if(ss.rie__Meeting_Password__c != Null){
            reqMap.put('password', ss.rie__Meeting_Password__c);
        }else{
            Integer len = 10;
            Blob blobkey = crypto.generateAesKey(128);
            String key = EncodingUtil.convertToHex(blobkey);
            String pwd = key.substring(0,len);
            reqMap.put('password', pwd);
        }
        reqMap.put('agenda', ss.Name);
        reqMap.put('recurrence',recurrenceMap);
        reqMap.put('settings', settingMap);
        reqMap.put('registrants_email_notification', 'true');
        String  reqBody = JSON.serialize(reqMap);
        ZoomService.createMeeting(reqBody, zoomUserId, ss.Id);
    }
    
    public static Map<String, String> getDifferentTimeZones(String eventTimeZone){
        String timezoneName = eventTimeZone.substringAfterLast('(').substringBeforeLast(')');
        String timeZoneTime = eventTimeZone.substringAfter('(GMT').substringBefore(')');
        String timeZoneSign = timeZoneTime.substring(0, 1);
        String timeZoneHour = timeZoneSign + timeZoneTime.substring(1, 3);
        String timeZoneMinute = timeZoneSign + timeZoneTime.substring(4, 6);
        Map<String , String> timeZoneMap = new Map<String, String>();
        timeZoneMap.put('timezoneName',timezoneName);
        timeZoneMap.put('timeZoneHour',timeZoneHour);
        timeZoneMap.put('timeZoneMinute',timeZoneMinute);
        return timeZoneMap;
     }
 }
I have created a test class but i am not gettting enoungh coverage and its failing with null pointer exception on bold lines.
my test class is given below:-
@isTest
private class SessionTriggerHelperTest {
    
    private class Mock implements HttpCalloutMock {
        
        public HTTPResponse respond(HTTPRequest req) {
            
            HTTPResponse res = new HTTPResponse();
            
            res.setStatusCode(200);
            res.setBody('{'+
        '  \"agenda\": \"Test Webinar\",'+
        '  \"created_at\": \"2020-04-15T11:16:58Z\",'+
        '  \"duration\": 60,'+
        '  \"host_id\": \"BhN6PRjkQW-iUc8-8-aTSw\",'+
        '  \"id\": 94298132774,'+
        '  \"join_url\": \"https://zoom.us/j/94298132774?pwd=REZsT1NnOFd5aWhUUDB0cTFRS010dz09\",'+
        '  \"password\": \"avfhfgh\",'+
        '  \"registration_url\": \"https://zoom.us/webinar/register/WN_BRg795wbTHSIOWEnknY5gA\",'+
        '  \"settings\": {'+
        '    \"allow_multiple_devices\": false,'+
        '    \"alternative_hosts\": \"\",'+
        '    \"approval_type\": 0,'+
        '    \"audio\": \"both\",'+
        '    \"auto_recording\": \"none\",'+
        '    \"close_registration\": true,'+
        '    \"contact_email\": \"steve@realintelligence.com\",'+
        '    \"contact_name\": \"Steve Kompolt\",'+
        '    \"enforce_login\": false,'+
        '    \"enforce_login_domains\": \"\",'+
        '    \"global_dial_in_countries\": ['+
        '      \"US\"'+
        '    ],'+
        '    \"global_dial_in_numbers\": ['+
        '      {'+
        '        \"city\": \"Chicago\",'+
        '        \"country\": \"US\",'+
        '        \"country_name\": \"US\",'+
        '        \"number\": \"+1 3126266799\",'+
        '        \"type\": \"toll\"'+
        '      },'+
        '      {'+
        '        \"city\": \"New York\",'+
        '        \"country\": \"US\",'+
        '        \"country_name\": \"US\",'+
        '        \"number\": \"+1 6465588656\",'+
        '        \"type\": \"toll\"'+
        '      },'+
        '      {'+
        '        \"city\": \"Houston\",'+
        '        \"country\": \"US\",'+
        '        \"country_name\": \"US\",'+
        '        \"number\": \"+1 3462487799\",'+
        '        \"type\": \"toll\"'+
        '      },'+
        '      {'+
        '        \"city\": \"San Jose\",'+
        '        \"country\": \"US\",'+
        '        \"country_name\": \"US\",'+
        '        \"number\": \"+1 6699009128\",'+
        '        \"type\": \"toll\"'+
        '      },'+
        '      {'+
        '        \"city\": \"\",'+
        '        \"country\": \"US\",'+
        '        \"country_name\": \"US\",'+
        '        \"number\": \"+1 2532158782\",'+
        '        \"type\": \"toll\"'+
        '      },'+
        '      {'+
        '        \"city\": \"\",'+
        '        \"country\": \"US\",'+
        '        \"country_name\": \"US\",'+
        '        \"number\": \"+1 3017158592\",'+
        '        \"type\": \"toll\"'+
        '      }'+
        '    ],'+
        '    \"hd_video\": true,'+
        '    \"host_video\": true,'+
        '    \"meeting_authentication\": false,'+
        '    \"on_demand\": false,'+
        '    \"panelists_video\": true,'+
        '    \"post_webinar_survey\": false,'+
        '    \"practice_session\": true,'+
        '    \"question_answer\": true,'+
        '    \"registrants_confirmation_email\": true,'+
        '    \"registrants_email_notification\": true,'+
        '    \"registrants_restrict_number\": 0,'+
        '    \"show_share_button\": true'+
        '  },'+
        '  \"start_time\": \"2020-09-20T06:59:00Z\",'+
        '  \"start_url\": \"https://zoom.us/s/94298132774?zak=eyJ6bV9za20iOiJ6bV9vMm0iLCJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJjbGllbnQiLCJ1aWQiOiJCaE42UFJqa1FXLWlVYzgtOC1hVFN3IiwiaXNzIjoid2ViIiwic3R5IjoxMDAsIndjZCI6ImF3MSIsImNsdCI6MCwic3RrIjoicm5JSzZ3VDNsQnh2ODdMQUg0RTFsZU1OOVFwcldNZ2VIMUhzWnRaNXE3NC5CZ1VzZDBWRWFUUk9lVWR1VkVSWGIxbFpWRkZNVGpKUmRsQldhWHBQZG5OT2J6bHFabmR6TVZaUVNtVkRTVDFBWkRZeVpEZ3hOMll3WVdFMk16WTRNalJsWldRd1pETXhZakZoWldJMk1tWTFNR1UyTXpFNU5HVXdNalZrWXpnek56WTFNR05oTUdSbU1EWXdZMll6T0FBTU0wTkNRWFZ2YVZsVE0zTTlBQU5oZHpFIiwiZXhwIjoxNTg2OTU2NjE4LCJpYXQiOjE1ODY5NDk0MTgsImFpZCI6ImtCQnVrUUQwUjRlaE9Lb00wWU80TmciLCJjaWQiOiIifQ.t-ECUWaekV1eRkBwA00XR0ysKz0OHSUagnwT7guXkg8\",'+
        '  \"timezone\": \"America/Los_Angeles\",'+
        '  \"topic\": \"Test Webinar\",'+
        '  \"type\": 5,'+
        '  \"uuid\": \"iTfCvg3iSQW3oFjwObnLog==\"'+
        '}');
            
            return res; 
        }
    }
    
    static testmethod void createWebinarTest(){
        Test.setMock(HttpCalloutMock.class, new Mock());
        
        rie__Event__c evt = new rie__Event__c();
        evt.rie__Event_Time_Zone__c = '(GMT-05:00) Eastern Standard Time (America/New_York)';
        insert evt;
        
        Session__c s = new Session__c();
        s.rie__Session_Status__c = 'Ready to Publish';
        s.rie__Date_Time__c = System.today();
        s.rie__Session_End_Time__c = System.today();
        s.rie__Event__c =  evt.id;
        insert s;
        Session__c st  =  [select rie__Zoom_URL__c,rie__Session_End_Time__c,rie__Date_Time__c,rie__Event_Time_Zone__c,rie__ZoomUserId__c 
                           from Session__c 
                           where id =: s.id];
       
        Test.startTest();
        string userid = st.rie__ZoomUserId__c;
        String eventTimeZone = st.rie__Event_Time_Zone__c;
        SessionTriggerhelper.getDifferentTimeZones(eventTimeZone);
        SessionTriggerhelper.createMeeting(userid,s);
        SessionTriggerhelper.createWebinar(userid,s);
         SessionTriggerhelper.createWebinarAndMeeting();

         Test.stopTest();
    }
    
    static testmethod void createMeetingTest(){
        Test.setMock(HttpCalloutMock.class, new Mock());
        
        rie__Event__c evt = new rie__Event__c();
        evt.rie__Event_Time_Zone__c = '(GMT-05:00) Eastern Standard Time (America/New_York)';
        insert evt;
        
        Session__c s = new Session__c();
        s.rie__Session_Status__c = 'Ready to Publish';
        s.rie__Date_Time__c = System.today();
        s.rie__Event__c =  evt.id;
        insert s;
        Session__c st  =  [select rie__Zoom_URL__c,rie__Date_Time__c,rie__Event_Time_Zone__c,rie__ZoomUserId__c 
                           from Session__c 
                           where id =: s.id];
       
        Test.startTest();
        string userid = st.rie__ZoomUserId__c;
        String eventTimeZone = st.rie__Event_Time_Zone__c;
        SessionTriggerhelper.getDifferentTimeZones(eventTimeZone);
        SessionTriggerhelper.createWebinarAndMeeting();
        SessionTriggerhelper.createWebinar(userid,s);
        SessionTriggerhelper.createMeeting(userid,s);
         Test.stopTest();
    }
}
Can anyone help me with this test class?
Best Answer chosen by sumit d
ShirishaShirisha (Salesforce Developers) 
Hi Sumit,

I would suggest you to create the debug logs while running the test class and capture to see,which part of the code causing the issue.

If needed add the system.debug() statements to check which variable is passing the null value to resolve the issue.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Sumit,

Greetings!

It is very difficult to provide the exact code to your code.However,I can provide you the best practices on test classes along with the examples which will make you comfort in writing the test classes.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

https://www.forcetalks.com/blog/salesforce-apex-test-class-best-practices/

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
sumit dsumit d
Hi Shrisha,
I have a  trigger helper class given below:-
public without sharing class RegistrationTriggerHelper {
    public static List<rie__Registration__c> newRegistration = new List<rie__Registration__c>();
    public static List<rie__Registration__c> oldRegistration = new List<rie__Registration__c>(); 
    public static Map<Id, rie__Registration__c> newMapRegistration =new Map<Id, rie__Registration__c>();
    public static Map<Id, rie__Registration__c> oldMapRegistration =new Map<Id, rie__Registration__c>();
    
    public static boolean runTrigger = TRUE;
    
    public static void createMeeting(){
        for(rie__Registration__c registrationObj : newRegistration){
            if(registrationObj.rie__Zoom_URL__c == Null 
               && registrationObj.rie__Status__c == 'Confirmed'){
                   String zoomUserId = '';
                   
                   if(registrationObj.rie__Zoom_User_Id__c     != Null){
                       zoomUserId = registrationObj.rie__Zoom_User_Id__c; 
                   }else{
                       zoomUserId = Label.Zoom_User_Id; 
                   }
                   
                   Map<String, Object> reqMap = new Map<String, Object>();
                   Map<String, Object> recurrenceMap = new Map<String, Object>();
                   recurrenceMap.put('type' , 3);
                   recurrenceMap.put('repeat_interval' , 1);
                   recurrenceMap.put('weekly_days' , '4');
                   recurrenceMap.put('monthly_day' , 10);
                   recurrenceMap.put('monthly_week' , 2);
                   //recurrenceMap.put('end_date_time' , registrationObj);
                   
                   Map<String, Object> settingMap = new Map<String, Object>();
                   settingMap.put('host_video' , false);
                   settingMap.put('participant_video' , false);
                   settingMap.put('cn_meeting' , false);
                   settingMap.put('in_meeting' , false);
                   settingMap.put('join_before_host' , true);
                   settingMap.put('mute_upon_entry' , false);
                   settingMap.put('watermark' , false);
                   settingMap.put('waiting_room' , false);
                   settingMap.put('use_pmi' , false);
                   settingMap.put('approval_type' , 0);
                   settingMap.put('registration_type' , 2);
                   settingMap.put('audio' , 'both');
                   settingMap.put('auto_recording' , 'none');
                   
                   reqMap.put('topic', registrationObj.rie__Job_Title__c);
                   reqMap.put('type', 3);
                   DateTime startTime = registrationObj.rie__Event_Start_Time__c.addHours(-7);
                   Map<String, String> timeZoneMap = SessionTriggerHelper.getDifferentTime
Zones(registrationObj.rie__Event_Time_Zone__c);
                   Integer timeZoneHour = Integer.valueOf(timeZoneMap.get('timeZoneHour'));
                   Integer timeZoneMinute = Integer.valueOf(timeZoneMap.get('timeZoneMinute'));
                   reqMap.put('start_time', startTime.addHours(timeZoneHour).addMinutes(timeZoneMinute));
                   if(registrationObj.rie__Event_Start_Time__c != Null && registrationObj.rie__Event_End_Date__c != Null){
                       Double st = DateTime.newInstanceGmt(registrationObj.rie__Event_End_Date__c.year(), 
                                                           registrationObj.rie__Event_End_Date__c.month(), 
                                                           registrationObj.rie__Event_End_Date__c.day()).getTime() - registrationObj.rie__Event_Start_Time__c.getTime();
                       st = st/(1000*60);
                       reqMap.put('duration', Integer.valueOf(st));
                   }
                   
                   reqMap.put('timezone', timeZoneMap.get('timezoneName'));
                   if(registrationObj.rie__Meeting_Password__c != Null){
                       reqMap.put('password', registrationObj.rie__Meeting_Password__c);
                   }else{
                       Integer len = 10;
                       Blob blobkey = crypto.generateAesKey(128);
                       String key = EncodingUtil.convertToHex(blobkey);
                       String pwd = key.substring(0,len);
                       reqMap.put('password', pwd);
                   }
                   reqMap.put('agenda', registrationObj.Name);
                   reqMap.put('recurrence',recurrenceMap);
                   reqMap.put('settings', settingMap);
                   reqMap.put('registrants_email_notification', 'true');
                   String  reqBody = JSON.serialize(reqMap);
                   ZoomService.createMeeting(reqBody, zoomUserId, registrationObj.Id, 'Registration');
               }
        } 
    }
}
Its test class is given below:-
@isTest
private class RegistrationTriggerHelperTest {
      static testmethod void createMeetingTest(){
          
        rie__Event__c evt = new rie__Event__c();
        evt.rie__Event_Time_Zone__c = '(GMT-05:00) Eastern Standard Time (America/New_York)';
        insert evt;
          
        rie__Registration__c registrationObj = new rie__Registration__c();
        registrationObj.rie__Zoom_URL__c = '';
        registrationObj.rie__Status__c = 'Confirmed';
        registrationObj.rie__Event__c = evt.id;
        insert registrationObj;
        test.startTest();
        RegistrationTriggerHelper.createMeeting();
        test.stopTest();
    }
}
It has null pointer exception on the bold lines in helper class.Can you help me how can i solve this error?

 
ShirishaShirisha (Salesforce Developers) 
Hi Sumit,

I would suggest you to create the debug logs while running the test class and capture to see,which part of the code causing the issue.

If needed add the system.debug() statements to check which variable is passing the null value to resolve the issue.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
This was selected as the best answer