+ Start a Discussion
Lavanya Ponniah 3Lavanya Ponniah 3 

How to improve the code coverage for the below code?

trigger Leaves on Staff_Leaves__c(before insert, before update) {
integer noofdays;
string half;
string full;
integer k = 0;

List < Event > listevent = new list < event > ();
map < Date, date > listholy = new map < date, date > ();
map < date, date > liststart = new map < date, date > ();
list < user > listuserupdate = new list < user > ();

if ((trigger.isbefore && trigger.isinsert) || (trigger.isbefore && trigger.isupdate)) {
  for (Staff_Leaves__c sl: trigger.new) {
   user u = [select Medical_Leave_Half__c, Hospitalisation_Half__c, Dependant_care_Half__c, Compassionate_leave_Half__c, Child_care_Half__c, Annual_Leave_Half__c, Annual_Leave__c, Child_care__c, Compassionate_leave__c, Dependant_care__c, Hospitalisation__c, Medical_Leave__c from user where id = : sl.Staff__c];

   list < event > le = [select startdatetime from event where Leave_Type__c = 'Leave'];
   for (event elist: le) {
    date da = elist.StartDateTime.date();
    liststart.put(da, da);
   }

   if (sl.Status1__c == 'Submited') {
    for (integer j = 0; j < sl.Between_Days__c; j++) {
     date date1 = sl.from__c + j;
     date date3 = sl.to__c + j;
     date date2 = date.ValueOf('2015-11-1');
     integer numberDaysDue = date2.daysBetween(date1);

     if ((listholy.containsKey(date1)) || (math.MOD(numberDaysDue, 7) == 0) || (math.MOD(numberDaysDue, 7) == 6)) {

     } else {
      k += 1;
     }
     sl.No_of_business_days1__c = k;
    }

    full = 'true';
    half = 'false';
   }
   if (sl.half_day1__C == true) {
    if ((sl.from__C.daysBetween(sl.To__c)) == 0) {
     if (liststart.containsKey(sl.From__c)) {
      sl.adderror('Leave already apply one of selected day(s)');
     } else {
      if (sl.Leave_Type__c == 'Annual Leave') {
       if (u.Annual_Leave_Half__c == 1) {
        if (u.Annual_Leave__c - sl.No_of_business_days1__c < 0) {
         sl.addError('You do not have enough leaves to apply');
        }
       } else if (u.Annual_Leave_Half__c == 0) {
        if (u.Annual_Leave__c - (sl.No_of_business_days1__c + 1) < 0) {
         sl.addError('You do not have enough leaves to apply');
        }
       }
      } else if (sl.Leave_Type__c == 'Child care') {
       if (u.Child_care_Half__c == 1) {
        if (u.Child_care__c - sl.No_of_business_days1__c < 0) {
         sl.addError('You do not have enough leaves to apply');
        }
       } else if (u.Child_care_Half__c == 0) {
        if (u.Child_care__c - (sl.No_of_business_days1__c + 1) < 0) {
         sl.addError('You do not have enough leaves to apply');
        }
       }
      } else if (sl.Leave_Type__c == 'Compassionate leave') {
       if (u.Compassionate_leave_Half__c == 1) {
        if (u.Compassionate_leave__c - sl.No_of_business_days1__c < 0) {
         sl.addError('You do not have enough leaves to apply');
        }
       } else if (u.Compassionate_leave_Half__c == 0) {
        if (u.Compassionate_leave__c - (sl.No_of_business_days1__c + 1) < 0) {
         sl.addError('You do not have enough leaves to apply');
        }
       }
      } else if (sl.Leave_Type__c == 'Dependant care') {
       if (u.Dependant_care_Half__c == 1) {
        if (u.Dependant_care__c < sl.No_of_business_days1__c) {
         sl.addError('You do not have enough leaves to apply');
        }
       } else if (u.Dependant_care_Half__c == 0) {
        if (u.Dependant_care__c < (sl.No_of_business_days1__c + 1)) {
         sl.addError('You do not have enough leaves to apply');
        }
       }
      } else if (sl.Leave_Type__c == 'Hospitalisation') {
       if (u.Hospitalisation_Half__c == 1) {
        if (u.Hospitalisation__c < sl.No_of_business_days1__c) {
         sl.addError('You do not have enough leaves to apply');
        }
       } else if (u.Hospitalisation_Half__c == 0) {
        if (u.Hospitalisation__c < (sl.No_of_business_days1__c + 1)) {
         sl.addError('You do not have enough leaves to apply');
        }
       }
      } else if (sl.Leave_Type__c == 'Medical Leave') {
       if (u.Medical_Leave_Half__c == 1) {
        if (u.Medical_Leave__c < sl.No_of_business_days1__c) {
         sl.addError('You do not have enough leaves to apply');
        }
       }
       if (u.Medical_Leave_Half__c == 0) {
        if (u.Medical_Leave__c < (sl.No_of_business_days1__c + 1)) {
         sl.addError('You do not have enough leaves to apply');
        }
       }
      }
     }

     half = 'true';
     full = 'false';
     sl.Half_day__c = 1;
     sl.No_of_business_days1__c = 0;
    } else sl.adderror('Please check the dates, between days more then 1');
   } else if (sl.Half_day1__c == false) {
    if (liststart.containsKey(sl.From__c)) {
     sl.adderror('Leave already apply one of selected day(s)');
    } else {
     if (u.id == sl.Staff__c) {
      system.debug('------------------------=' + u.Annual_Leave__c);
      if (sl.Leave_Type__c == 'Annual Leave') {
       //integer in1=u.Annual_Leave__c-sl.No_of_business_days1__c;
       system.debug('...............................=' + u.Annual_Leave__c);
       system.debug('...............................=' + sl.No_of_business_days1__c);
       //system.debug('...............................=' + in1);
       if (u.Annual_Leave__c - sl.No_of_business_days1__c < 0) {
        system.debug(',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,');
        sl.addError('You do not have enough leaves to apply');
       }
      } else if (sl.Leave_Type__c == 'Child care') {
       if (u.Child_care__c < sl.No_of_business_days1__c) {
        sl.addError('You do not have enough leaves to apply');
       }
      } else if (sl.Leave_Type__c == 'Compassionate leave') {
       if (u.Compassionate_leave__c < sl.No_of_business_days1__c) {
        sl.addError('You do not have enough leaves to apply');
       }
      } else if (sl.Leave_Type__c == 'Dependant care') {
       if (u.Dependant_care__c < sl.No_of_business_days1__c) {
        sl.addError('You do not have enough leaves to apply');
       }
      } else if (sl.Leave_Type__c == 'Hospitalisation') {
       if (u.Hospitalisation__c < sl.No_of_business_days1__c) {
        sl.addError('You do not have enough leaves to apply');
       }
      } else if (sl.Leave_Type__c == 'Medical Leave') {
       if (u.Medical_Leave__c < sl.No_of_business_days1__c) {
        sl.addError('You do not have enough leaves to apply');
       }
      }
     }
    }
    list < Holiday > holy = [select ActivityDate from Holiday];
    for (Holiday h: holy) {
     date d = h.ActivityDate;
     listholy.put(d, d);
    }


   }


   if (sl.Status1__c == 'Approved') {
    if (sl.Half_day1__c == true) {
     if (sl.Leave_Type__c == 'Annual Leave') {
      if (u.Annual_Leave_Half__c == 1) {
       u.Annual_Leave__c -= sl.No_of_business_days1__c;
       u.Annual_Leave_Half__c = 0;
       update u;
      } else if (u.Annual_Leave_Half__c == 0) {
       u.Annual_Leave__c -= (sl.No_of_business_days1__c + 1);
       u.Annual_Leave_Half__c = 1;
       update u;
      }
     } else if (sl.Leave_Type__c == 'Child care') {
      if (u.Child_care_Half__c == 1) {
       u.Child_care__c -= sl.No_of_business_days1__c;
       u.Child_care_Half__c = 0;
       update u;
      } else if (u.Child_care_Half__c == 0) {
       u.Child_care__c -= (sl.No_of_business_days1__c + 1);
       u.Child_care_Half__c = 1;
       update u;
      }
     } else if (sl.Leave_Type__c == 'Compassionate leave') {
      if (u.Compassionate_leave_Half__c == 1) {
       u.Compassionate_leave__c -= sl.No_of_business_days1__c;
       u.Compassionate_leave_Half__c = 0;
       update u;
      } else if (u.Compassionate_leave_Half__c == 0) {
       u.Compassionate_leave__c -= (sl.No_of_business_days1__c + 1);
       u.Compassionate_leave_Half__c = 1;
       update u;
      }
     } else if (sl.Leave_Type__c == 'Dependant care') {
      if (u.Dependant_care_Half__c == 1) {
       u.Dependant_care__c -= sl.No_of_business_days1__c;
       u.Dependant_care_Half__c = 0;
       update u;
      } else if (u.Dependant_care_Half__c == 0) {
       u.Dependant_care__c -= (sl.No_of_business_days1__c + 1);
       u.Dependant_care_Half__c = 1;
       update u;
      }
     } else if (sl.Leave_Type__c == 'Hospitalisation') {
      if (u.Hospitalisation_Half__c == 1) {
       u.Hospitalisation__c -= sl.No_of_business_days1__c;
       u.Hospitalisation_Half__c = 0;
       update u;
      } else if (u.Hospitalisation_Half__c == 0) {
       u.Hospitalisation__c -= (sl.No_of_business_days1__c + 1);
       u.Hospitalisation_Half__c = 1;
       update u;
      }
     } else if (sl.Leave_Type__c == 'Medical Leave') {
      if (u.Medical_Leave_Half__c == 1) {
       u.Medical_Leave__c -= sl.No_of_business_days1__c;
       u.Medical_Leave_Half__c = 0;
       update u;
      }
      if (u.Medical_Leave_Half__c == 0) {
       u.Medical_Leave__c -= (sl.No_of_business_days1__c + 1);
       u.Medical_Leave_Half__c = 1;
       update u;
      }
     }

     event eve = new event();
     eve.Subject = sl.Leave_Type__c;
     eve.StartDateTime = datetime.newInstance((sl.To__c).year(), (sl.To__c).month(), (sl.To__c).day()).addhours(8);
     eve.endDateTime = datetime.newInstance((sl.from__c).year(), (sl.from__c).month(), (sl.from__c).day()).addhours(17);
     eve.DurationInMinutes = null;
     eve.Leave_Type__c = 'Leave';
     insert eve;
    }


    if (sl.Half_day1__c == false) {
     if ((sl.Leave_Type__c == 'Annual Leave') && (u.Annual_Leave__c > 0)) {
      u.Annual_Leave__c -= sl.No_of_business_days1__c;
      update u;
     } else if ((sl.Leave_Type__c == 'Child care') && (u.Child_care__c > 0)) {
      u.Child_care__c -= sl.No_of_business_days1__c;
      update u;
     } else if ((sl.Leave_Type__c == 'Compassionate leave') && (u.Compassionate_leave__c > 0)) {
      u.Compassionate_leave__c -= sl.No_of_business_days1__c;
      update u;
     } else if ((sl.Leave_Type__c == 'Dependant care') && (u.Dependant_care__c > 0)) {
      u.Dependant_care__c -= sl.No_of_business_days1__c;
      update u;
     } else if ((sl.Leave_Type__c == 'Hospitalisation') && (u.Hospitalisation__c > 0)) {
      u.Hospitalisation__c -= sl.No_of_business_days1__c;
      update u;
     } else if ((sl.Leave_Type__c == 'Medical Leave') && (u.Medical_Leave__c > 0)) {
      u.Medical_Leave__c -= sl.No_of_business_days1__c;
      update u;
     }

     integer q = -1;
     for (integer a = 0; a < sl.Between_Days__c / 14; a++) {
      integer b = 0;
      system.debug('-------------integer =' + b);
      for (b = 0; b < 14; b++) {
       q += 1;
       if (q < sl.Between_Days__c) {
        event eve = new event();
        eve.Subject = sl.Leave_Type__c;
        eve.StartDateTime = datetime.newInstance((sl.To__c).year(), (sl.To__c).month(), (sl.To__c).day() + q).addhours(8);
        eve.endDateTime = datetime.newInstance((sl.to__c).year(), (sl.to__c).month(), (sl.to__c).day() + q).addhours(17);
        eve.DurationInMinutes = null;
        eve.Leave_Type__c = 'Leave';
        listevent.add(eve);
       }
       system.debug('------------- b=' + b);
      }
     }
     if (listevent.size() > 0) {
      insert listevent;
     }
     listevent.clear();
    }
   }
  }
}
}


Test Class:
@istest
public class TestLeaves
{
static testmethod void  testLeaves1()
    {
        map<Date,date> listholy=new map<date,date>();
          Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
       
        User u = new User(
            Alias = 'Anusiah',
         Email='swarna@ideallsf.com',
            EmailEncodingKey='General US & Western Europe (ISO-8859-1, ISO-LATIN-1)',
            LastName='Anusiah',
         LanguageLocaleKey='English',
            LocaleSidKey='en_US',
         ProfileId = p.Id ,
            TimeZoneSidKey='English (Singapore)',
         UserName='admin@dpa.org.sg.dpa',
         Medical_Leave_Half__c=2,
         Hospitalisation_Half__c=1,
         Dependant_care_Half__c=1,
         Compassionate_leave_Half__c=1,
         Child_care_Half__c=1,
         Annual_Leave_Half__c=1,
         Annual_Leave__c=1,
         Child_care__c=1,
         Compassionate_leave__c=1,
         Dependant_care__c=1,
         Hospitalisation__c=1,
         Medical_Leave__c=1);
       
       
        Staff_Leaves__c  sl=new Staff_Leaves__c ();
         sl.Leave_Type__c='Medical Leave';
         sl.to__c=date.today();
         sl.from__c=date.today();
         sl.Staff__c=u.id;
         sl.Half_day1__c=true;
         sl.Test__c='Approved';
         sl.Staff__c='005200000010IK0AAM';
        insert sl;
       
       
       event eee=new event();
         eee.Subject=sl.Leave_Type__c;
            eee.StartDateTime=date.today();
            eee.endDateTime=date.today();
            eee.DurationInMinutes= null;
            eee.Leave_Type__c='Leave';
        insert eee;
       
        Holiday h1=new Holiday(ActivityDate=date.today()+5, Name='test');
        
   
       
        list<Holiday> holy=[select ActivityDate from Holiday where ActivityDate=:date.today()+5];
        for(Holiday h:holy)
        {
            date d=h.ActivityDate;
          listholy.put(d,d); 
        }
       
       test.startTest();
        
         for(integer j=0;j<sl.Between_Days__c;j++)
         {
              date date1=sl.Start__c.date()+j;
              date date3=sl.end__c.date()+j;
              date date2= date.ValueOf('2015-11-1');
              integer numberDaysDue = date2.daysBetween(date1);
                integer k=0;
              
             if((listholy.containsKey(date1)) || (math.MOD( numberDaysDue,7)==0 ) ||(math.MOD( numberDaysDue,7)==6 ))
             {
                      
             }
                else
              {
                  k+=1;
              }
                sl.No_of_business_days1__c=k;
            }
            
              
        
         user u1=[select Medical_Leave_Half__c,Hospitalisation_Half__c,Dependant_care_Half__c,Compassionate_leave_Half__c,Child_care_Half__c,Annual_Leave_Half__c,Annual_Leave__c,Child_care__c,Compassionate_leave__c,Dependant_care__c,Hospitalisation__c,Medical_Leave__c from user where id =:'005200000010IK0AAM'];           
         system.assertEquals(u1.id, sl.Staff__c);
         event le=[select startdatetime from event where Leave_Type__c='Leave'];
          
       test.stopTest();
    }
   
It is covering only 19% 40 lines.
}
James LoghryJames Loghry
Most of your trigger is a lengthy series of if else statements.  Each of these branches (and their conditions) need to be met in your test class if you hope to achieve anywhere near 100% code coverage.  Hence, the best way of improving your code coverage is to refactor the trigger to reduce the if / else statements, potentially by utilizing maps or other mechanisms for simplifying your trigger.