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
The new LearnerThe new Learner 

NOt able to cover the code coverage for the below class

Hi Experts,

I am not Not able to cover the below class handler which works before update , can anyone help me out. please. still its only 53%, majority of the issue is wiht else part which i bolded.
 
public without sharing class Stamp_Case
{
    
    public static void updateBeforeChange(List<SObject> newSobjs, Map<Id,SObject> oldSobjsMap)
    {
        List<Case> newCases = (List<Case>) newSobjs;
        Map<Id, Case> oldCasesMap = (Map<Id, Case>)oldSobjsMap;
        BusinessHours bh = [SELECT Id,SundayStartTime, MondayStartTime, TuesdayStartTime,WednesdayStartTime, ThursdayStartTime, FridayStartTime,SaturdayStartTime, SundayEndTime, MondayEndTime,TuesdayEndTime,WednesdayEndTime, ThursdayEndTime, FridayEndTime,SaturdayEndTime FROM BusinessHours WHERE IsDefault=true];//Quering the businesshours to user in IsWithin function
        system.debug('BusinessHoursBusinessHours  '+bh);
        for(Case cs : newCases)
        {
            Case oldCs = oldCasesMap.get(cs.Id);
            Boolean oldrecordtype= oldcs.RecordType_Name__c.equals('N_L_QNA');
            Boolean oldrecordtype1= oldcs.RecordType_Name__c.equals('L_QNA');
            Boolean newrecordtype = cs.RecordType_Name__c.equals('UW_L_QNA');
            Boolean newrecordtype1= cs.RecordType_Name__c.equals('N_L_QNA');
            string oldCsstatus = oldCasesMap.get(cs.Id).Status; 
            string dayOfWeek=cs.lastmodifieddate.format('EEEE');
            System.debug('Day : ' + dayOfWeek);
            DateTime dT = cs.LastModifiedDate;
            Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
            System.debug('myDate : ' + myDate );
            String Mon ='Monday';
            String Tue ='Tuesday';
            String Wed ='Wednesday';
            String Thur ='Thursday';
            String Fri ='Friday';
            String Sat= 'Saturday';
            String Sun= 'Sunday';
            
            
            Boolean isWithin = BusinessHours.isWithin(bh.id,  cs.LastModifiedDate);
            
            
            if((oldrecordtype  || oldrecordtype1 ) && (newrecordtype || newrecordtype1) )
            {
                if(isWithin==true && (dayOfWeek == Mon || dayOfWeek == Tue|| dayOfWeek == Wed|| dayOfWeek == Thur|| dayOfWeek == Fri))
                {
                    
                   
                    cs.Sent_Hours__c= B_BusinessHours.getTimeDifferenceInMinutes(
                        cs.CreatedDate,
                        cs.LastModifiedDate,
                        businessHoursId);
                  
                    
                    Decimal mydecval = cs.Sent_Formula__c;
                    system.debug('mydecvalmydecvalmydecval'+mydecval);
                    Integer myintval = mydecval.intValue(); //Converting Decimal Value to Integer
                    system.debug('myintvalmyintvalmyintval'+myintval);
                    
                   
                    cs.Sent_to__c = B_BusinessHours.addMinutes(
                        myintval,
                        businessHoursId);
                }
                
                **else if (dayOfWeek == Mon)
                {
                    cs.Sent_to__c= DateTime.newInstance(myDate,bh.MondayEndTime); 
                }
                else if(dayOfWeek == Tue)
                {
                    cs.Sent_to__c=  DateTime.newInstance(myDate,bh.TuesdayEndTime) ;
                }
                else if(dayOfWeek == Wed)
                {
                    cs.Sent_to__c=  DateTime.newInstance(myDate,bh.WednesdayEndTime) ;
                }
                else if(dayOfWeek == Thur)
                {
                    cs.Sent_to__c=  DateTime.newInstance(myDate,bh.ThursdayEndTime) ;
                   
                }
                else if(dayOfWeek == Fri)
                {
                    cs.Sent_to__c=  DateTime.newInstance(myDate,bh.FridayEndTime) ;
                }
                else if(dayOfWeek == Sat)
                {
                    Date satdate = myDate- 1;
                    cs.Sent_to__c=  DateTime.newInstance(satdate,bh.FridayEndTime) ;
                }
                else if(dayOfWeek == Sun)
                {
                    Date Sundate=myDate- 2;
                    cs.Sent_to__c=  DateTime.newInstance(Sundate,bh.FridayEndTime) ;**
                }
                
                
            }
            
            if((cs.status =='Emailed to Broker' || cs.status==' Emailed to Distribution') && oldCsstatus != cs.status)
            {
                **if(isWithin==true && (dayOfWeek == Mon || dayOfWeek == Tue|| dayOfWeek == Wed|| dayOfWeek == Thur|| dayOfWeek == Fri))
                {
                    cs.Dist_Hours__c = B_BusinessHours.getTimeDifferenceInMinutes(
                        cs.CreatedDate,
                        cs.LastModifiedDate,
                        businessHoursId);
                    
                    Decimal mydecval = cs.Dist_Formula__c;
                   
                    Integer myintval = mydecval.intValue();
                    system.debug('myintvalmyintvalmyintval'+myintval);
                    cs.Dist__c= B_BusinessHours.addMinutes(
                        myintval,
                        businessHoursId);
                }
                else if (dayOfWeek == Mon)
                {
                    cs.Dist__c= DateTime.newInstance(myDate,bh.MondayEndTime); 
                }
                else if(dayOfWeek == Tue)
                {
                    cs.Dist__c=  DateTime.newInstance(myDate,bh.TuesdayEndTime) ;
                }
                else if(dayOfWeek == Wed)
                {
                    cs.Dist__c=  DateTime.newInstance(myDate,bh.WednesdayEndTime) ;
                }
                else if(dayOfWeek == Thur)
                {
                    cs.Dist__c=  DateTime.newInstance(myDate,bh.ThursdayEndTime) ;
                  
                }
                else if(dayOfWeek == Fri)
                {
                    cs.Dist__c=  DateTime.newInstance(myDate,bh.FridayEndTime) ;
                }
                else if(dayOfWeek == Sat)
                {
                    Date satdate = myDate- 1;
                    cs.Dist__c=  DateTime.newInstance(satdate,bh.FridayEndTime) ;
                }
                else if(dayOfWeek == Sun)
                {
                    Date Sundate=myDate- 2;
                    cs.Dist__c=  DateTime.newInstance(Sundate,bh.FridayEndTime) ;
                }**
                
            }
            
            if(cs.Process__c=='Verify' && cs.Verify_Date__c== null)
            {
                
                if(isWithin==true && (dayOfWeek == Mon || dayOfWeek == Tue|| dayOfWeek == Wed|| dayOfWeek == Thur|| dayOfWeek == Fri))
                { 
                    
                    cs.Date_Hours__c= B_BusinessHours.getTimeDifferenceInMinutes(
                        cs.CreatedDate,
                        cs.LastModifiedDate,
                        businessHoursId);
                    
                    Decimal mydecval = cs.Date_Formula__c;
                  
                    Integer myintval = mydecval.intValue();
                   
                    cs.Verify_Date__c= B_BusinessHours.addMinutes(
                        myintval,
                        businessHoursId);
                }
                
                **else if (dayOfWeek == Mon)
                {
                    cs.Verify_Date__c = DateTime.newInstance(myDate,bh.MondayEndTime); 
                }
                else if(dayOfWeek == Tue)
                {
                    cs.Verify_Date__c =  DateTime.newInstance(myDate,bh.TuesdayEndTime) ;
                }
                else if(dayOfWeek == Wed)
                {
                    cs.Verify_Date__c =  DateTime.newInstance(myDate,bh.WednesdayEndTime) ;
                }
                else if(dayOfWeek == Thur)
                {
                    cs.Verify_Date__c =  DateTime.newInstance(myDate,bh.ThursdayEndTime) ;
                   
                }
                else if(dayOfWeek == Fri)
                {
                    cs.Verify_Date__c =  DateTime.newInstance(myDate,bh.FridayEndTime) ;
                }
                else if(dayOfWeek == Sat)
                {
                    Date satdate = myDate- 1;
                    cs.Verify_Date__c =  DateTime.newInstance(satdate,bh.FridayEndTime) ;
                }
                else if(dayOfWeek == Sun)
                {
                    Date Sundate=myDate- 2;
                    cs.Verify_Date__c =  DateTime.newInstance(Sundate,bh.FridayEndTime) ;**
                } 
                
            }
            
            if(cs.Process__c=='Preparation' && cs.Preparation_Date__c== null)
            {
                
                if(isWithin==true && (dayOfWeek == Mon || dayOfWeek == Tue|| dayOfWeek == Wed|| dayOfWeek == Thur|| dayOfWeek == Fri))
              {
                
                cs.Preparation_Date_Hours__c= B_BusinessHours.getTimeDifferenceInMinutes(
                    cs.CreatedDate,
                    cs.LastModifiedDate,
                    businessHoursId);
                
                Decimal mydecval = cs.Preparation_Date_Formula__c;
                
                Integer myintval = mydecval.intValue();
                
                cs.Preparation_Date__c = B_BusinessHours.addMinutes(
                    myintval,
                    businessHoursId);
                
            }
            
            else if (dayOfWeek == Mon)
            {
                **cs.Preparation_Date__c = DateTime.newInstance(myDate,bh.MondayEndTime);** 
            }
            else if(dayOfWeek == Tue)
            {
                **cs.Preparation_Date__c =  DateTime.newInstance(myDate,bh.TuesdayEndTime) ;**
            }
            else if(dayOfWeek == Wed)
            {
                **cs.Preparation_Date__c =  DateTime.newInstance(myDate,bh.WednesdayEndTime) ;**
            }
            else if(dayOfWeek == Thur)
            {
                cs.Preparation_Date__c =  DateTime.newInstance(myDate,bh.ThursdayEndTime) ;
               
            }
            **else if(dayOfWeek == Fri)
            {
                cs.Preparation_Date__c =  DateTime.newInstance(myDate,bh.FridayEndTime) ;
            }
            else if(dayOfWeek == Sat)
            {
                Date satdate = myDate- 1;
                cs.Preparation_Date__c =  DateTime.newInstance(satdate,bh.FridayEndTime) ;
            }
            else if(dayOfWeek == Sun)
            {
                Date Sundate=myDate- 2;
                cs.Preparation_Date__c =  DateTime.newInstance(Sundate,bh.FridayEndTime) ;**
            } 
            
          }
            
        }
        
    }
    
    public static Id businessHoursId
    {
        get
        {
            if (businessHoursId == null)
            {
                businessHoursId = B_BusinessHours.getBusinessHoursId();
            }
            return businessHoursId;
        }
        set;
    }
}


Below is my Test class.

        @isTest
    public class Case_Test{
    
    Public static testMethod void Test_method() 
    {
    
      Id recordtyepe= Schema.SObjectType.Case.getRecordTypeInfosByName().get('- L - QNA').getRecordTypeId();
      Id recordtyepes= Schema.SObjectType.Case.getRecordTypeInfosByName().get('UW - L - QNA').getRecordTypeId();
      List<BusinessHours> bhs=[select id from BusinessHours where IsDefault=true];
     
      case c = new case();
      c.Origin='email';
      c.Status='Assigned';
      c.RecordtypeId= recordtyepe;
      c.Process__c='Verify';
      c.Process__c= 'Preparation';
      insert c;
      c.Process__c='Preparation';
      c.Process__c='Verify';
      c.RecordtypeId =recordtyepes;
      Test.setCreatedDate(c.Id,  datetime.newInstance(2019, 11, 11, 11, 30, 0));
       Test.startTest();
       case cs= [SELECT Id, CreatedDate,LastModifiedDate FROM case  where origin='email' limit 1];
       System.assertEquals(cs.CreatedDate,  DateTime.newInstance(2019,11,11,11,30,0));
       Test.stopTest();
       update c;
     
    }
    }

 
ShirishaShirisha (Salesforce Developers) 
Hi,

Please use the below documents for creating the test classes:

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

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
The new LearnerThe new Learner
hi shirisha,

I knew how to write test class but i am not understanding , how to cover this else part, can you help me how to do that.