+ Start a Discussion
Mohan SelvamMohan Selvam 

Guide to write test class

Hi all
i am a Beginner 
Please can anyone guide me to write test class for the following. 

Description: Based on Object1's fields input i am updating Object2's field. i have achieved the reqirement. But i couldn't write test class for my method. 
Object1 fields : a)Arrival time    b)Departure time 
Object2 field:Outofhours.
My code as follows
Public string CalculateOutOfOfficeHour(datetime inTime, datetime outTime){
    Map<String, String> listDayMap = new Map<String, String>{'Monday' => 'Monday', 'Tuesday' => 'Tuesday','Wednesday' => 'Wednesday' , 'Thursday' => 'Thursday' ,'Friday' => 'Friday','Saturday'=>'Saturday','Sunday'=>'Sunday'};
    String StartdayOfWeek =inTime.format('EEEE');
    String EnddayOfWeek = outTime.format('EEEE');
    time start = Time.newInstance(8,30,0,0);
    time stop = Time.newInstance(17,0,0,0);
    string bookingStarttime = string.valueOf( Time.newInstance(inTime.hour(),inTime.minute(),inTime.second(),inTime.millisecond()));
    string bookingEndtime = string.valueOf(Time.newInstance(outTime.hour(),outTime.minute(),outTime.second(),outTime.millisecond()));
    Date mysDate = Date.newInstance(inTime.year(), inTime.month(), inTime.day());
    Date myeDate = Date.newInstance(outTime.year(), outTime.month(), outTime.day());
    String result='';     
    integer numberDaysDue = mysDate.daysBetween(myeDate);

  if(numberDaysDue >= 5 )
  {
     result='BOTH';
  }            
  if((numberDaysDue == 0))
  {
     if((StartdayOfWeek == 'Saturday'||StartdayOfWeek =='Sunday')){
             result='OUT';}
     else if((StartdayOfWeek == 'Monday')&&(bookingStarttime < string.valueOf(start))&&(bookingEndtime < string.valueOf(start))){
             result='OUT';}
     else if((StartdayOfWeek == 'Monday')&&(bookingStarttime <= string.valueOf(start))&&(bookingEndtime > string.valueOf(start))){
             result='BOTH';}
     else if((StartdayOfWeek == 'Friday')&&(bookingStarttime <= string.valueOf(start))&&(bookingEndtime > string.valueOf(stop))){
             result='BOTH';}
     else if((StartdayOfWeek == 'Friday')&&(bookingEndtime > string.valueOf(stop))&&(bookingStarttime > string.valueOf(stop))){
             result='OUT';}
     else{
             result='IN';}
  }

 return result ;
  
}    
 
Best Answer chosen by Mohan Selvam
John PipkinJohn Pipkin
All you really need to do is call this method and do some assertions. For example:
public static testMethod void myTest(){
    DateTime dtIn = system.now();
    DateTime dtOut = system.now().addDays(2);

    YourClassName ycn = new YourClassName();

    system.assertEquals('Your expected outcome', ycn.CalculateOutOfOfficeHour(dtIn,dtOut));
}

You'll need to replace the "Your expected outcome" and "YourClassName" with the correct values. Also, it's best to test several variables to make sure you hit every conditional statement. 

Hope that helps

All Answers

John PipkinJohn Pipkin
All you really need to do is call this method and do some assertions. For example:
public static testMethod void myTest(){
    DateTime dtIn = system.now();
    DateTime dtOut = system.now().addDays(2);

    YourClassName ycn = new YourClassName();

    system.assertEquals('Your expected outcome', ycn.CalculateOutOfOfficeHour(dtIn,dtOut));
}

You'll need to replace the "Your expected outcome" and "YourClassName" with the correct values. Also, it's best to test several variables to make sure you hit every conditional statement. 

Hope that helps
This was selected as the best answer
Mohan SelvamMohan Selvam
@John Pipkin

Thanks for your response 
John PipkinJohn Pipkin
No problem. Please mark as best answer to solve this question.