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
CaisaCaisa 

Testing

Hello,

I am brand new to Apex Testing and have a deadline this week. Can someone please tell me how to write the Apex Test for this:

trigger TruBidCompletion on Opportunity (before update)
{
public static Boolean IsWeekendDay(Date dateParam)
   {
      boolean result     = false;
      system.debug('dateParam = '+dateParam); 
      //Recover the day of the week
      Date startOfWeek   = dateParam.toStartOfWeek();
      system.debug('startOfWeek = '+startOfWeek);
      Integer dayOfWeek  = dateParam.day() - startOfWeek.day();
      system.debug('dayOfWeek = '+dayOfWeek);   
      result = dayOfWeek == 0 || dayOfWeek == 6 ? true : false;
      system.debug('result = '+result); 
      return result;
   } 
   
   
   public static Date AddBusinessDays(Date StartDate, integer BusinessDaysToAdd )
   {
      //Add or decrease in BusinessDaysToAdd days 
      Date finalDate = StartDate;
      system.debug('finaldate = '+finalDate);
      integer direction = BusinessDaysToAdd < 0 ? -1 : 1;
      system.debug('direction = '+direction);
       while(BusinessDaysToAdd != 0)
       {
           finalDate = finalDate.AddDays(direction);
           system.debug('BusinessDaysToAdd = '+BusinessDaysToAdd);            
           system.debug('finaldate = '+finalDate);
           if (!isWeekendDay(finalDate))
           {
               BusinessDaysToAdd -= direction;
               
           }
       }

       return finalDate;
   }
 
 
 {
  for(Opportunity o : Trigger.new)
    {
    /* If the user selects Round 1 as the Number of TruBid Rounds */
    if(o.Number_of_Rounds_in_TruBid__c=='Round 1')
    {
    /* Call the AddBusinessDays method called above to add 'x' days */
    o.Contract_Draft_Requested_Start_Date__c=o.Rd_1_Results_Review_End_Date__c;
    o.Contract_Draft_Requested_End_Date__c=AddBusinessDays( o.Contract_Draft_Requested_Start_Date__c,1);
    o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
    o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
    o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
    o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
    o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
    o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
    o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
    o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
    }
   
    
    else if 
    /* If the user selects Round 2 as the Number of TruBid Rounds */
    (o.Number_of_Rounds_in_TruBid__c=='Round 2')
    {
     /* Call the AddBusinessDays method called above to add 'x' days */
    o.Contract_Draft_Requested_Start_Date__c=o.Rd_2_Results_Review_End_Date__c;
    o.Contract_Draft_Requested_End_Date__c=AddBusinessDays(o.Contract_Draft_Requested_Start_Date__c,1);
    o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
    o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
    o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
    o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
    o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
    o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
    o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
    o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
   }
   
   else if 
   /* If the user selects Round 3 as the Number of TruBid Rounds */
    (o.Number_of_Rounds_in_TruBid__c=='Round 3')
    {
     /* Call the AddBusinessDays method called above to add 'x' days */
    o.Contract_Draft_Requested_Start_Date__c=o.Rd_3_Results_Review_End_Date__c;
    o.Contract_Draft_Requested_End_Date__c=AddBusinessDays(o.Contract_Draft_Requested_Start_Date__c,1);
    o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
    o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
    o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
    o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
    o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
    o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
    o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
    o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
   }
   
}
   
 } 
Best Answer chosen by Caisa
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
public class Test_TruBidCompletion {
    
    public static testmethod void test1()
    {
		Account acc= new Account();
		acc.name ='test';
		// add all required field here
		insert acc;
		
        Opportunity op = New Opportunity ();
		op.name ='TestOpportunity';
		op.CloseDate = system.today();
		op.StageName = 'Prospecting' ;
		op.accountid = acc.id;
		op.TB_Edits_in_Progress_Start_Date__c = System.today();
		op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
		op.Contract_Draft_Requested_Start_Date__c =  System.today();
		op.Award_Start_Date__c = System.today();
		op.TB_Contract_Signed_Start_Date__c = System.today();
        insert op;
		
        op.Number_of_Rounds_in_TruBid__c = 'Round 1';
        update op;
    }
    
    public static testmethod void test2()
    {
		Account acc= new Account();
		acc.name ='test';
		// add all required field here
		insert acc;
		
        Opportunity op = New Opportunity ();
		op.name ='TestOpportunity';
		op.CloseDate = system.today();
		op.StageName = 'Prospecting' ;
		op.accountid = acc.id;
		op.TB_Edits_in_Progress_Start_Date__c = System.today();
		op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
		op.Contract_Draft_Requested_Start_Date__c =  System.today();
		op.Award_Start_Date__c = System.today();
		op.TB_Contract_Signed_Start_Date__c = System.today();
        insert op;

        op.Number_of_Rounds_in_TruBid__c = 'Round 2';
        update op;
    }
    
    public static testmethod void test3()
    {
		Account acc= new Account();
		acc.name ='test';
		// add all required field here
		insert acc;
		
        Opportunity op = New Opportunity ();
		op.name ='TestOpportunity';
		op.CloseDate = system.today();
		op.StageName = 'Prospecting' ;
		op.accountid = acc.id;
		op.TB_Edits_in_Progress_Start_Date__c = System.today();
		op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
		op.Contract_Draft_Requested_Start_Date__c =  System.today();
		op.Award_Start_Date__c = System.today();
		op.TB_Contract_Signed_Start_Date__c = System.today();
        insert op;

        op.Number_of_Rounds_in_TruBid__c = 'Round 3';
        update op;
    }
}

 

All Answers

BALAJI CHBALAJI CH
Hi Caisa,

Please find below Test Class for above trigger.
@isTest
public class Test_TruBidCompletion {
    
    public static testmethod void test1()
    {
        Opportunity op = New Opportunity (name ='TestOpportunity', CloseDate = system.today(), StageName = 'Prospecting');
        insert op;
        op.Number_of_Rounds_in_TruBid__c = 'Round 1';
        update op;
    }
    
    public static testmethod void test2()
    {
        Opportunity op = New Opportunity (name ='TestOpportunity', CloseDate = system.today(), StageName = 'Prospecting');
        insert op;
        op.Number_of_Rounds_in_TruBid__c = 'Round 2';
        update op;
    }
    
    public static testmethod void test3()
    {
        Opportunity op = New Opportunity (name ='TestOpportunity', CloseDate = system.today(), StageName = 'Prospecting');
        insert op;
        op.Number_of_Rounds_in_TruBid__c = 'Round 3';
        update op;
    }
}

Let us know if that helps you.

Best Regards,
BALAJI
CaisaCaisa
Hi - this one fails - can you please help me figure out why?
BALAJI CHBALAJI CH
Can you please let me know if you are getting any exception or any error for the Fail ?
If possible, share a screenshot with detailed Error/Exception.
CaisaCaisa
Balaji - here is the screenshot. Not sure how to find the error/exception.

User-added image
CaisaCaisa
@Balaji - thank you very much for your time - I am very new to this. I will be away from here for an hour - will right back soon after
BALAJI CHBALAJI CH
Double click on that, a new tab will open with the details like below screenshot.
User-added image

Then click on Errors. The Error will popup on screen like below screenshot. User-added image

Please copy and paste that error here to improve the test class. Also let me know if there are any other Required fields in your Opportunity object.
CaisaCaisa
Balaji - I get this error: System.DmlException: Update failed. First exception on row 0 with id 00654000002kw8oAAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TruBidCompletion: execution of BeforeUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.TruBidCompletion: line 33, column 1
Trigger.TruBidCompletion: line 55, column 1: []
CaisaCaisa
Amit,

I get the same error when I run the code you suggested:

System.DmlException: Update failed. First exception on row 0 with id 00654000002kw8oAAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TruBidCompletion: execution of BeforeUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.TruBidCompletion: line 33, column 1
Trigger.TruBidCompletion: line 55, column 1: []
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
public class Test_TruBidCompletion {
    
    public static testmethod void test1()
    {
		Account acc= new Account();
		acc.name ='test';
		// add all required field here
		insert acc;
		
        Opportunity op = New Opportunity ();
		op.name ='TestOpportunity';
		op.CloseDate = system.today();
		op.StageName = 'Prospecting' ;
		op.accountid = acc.id;
		op.TB_Edits_in_Progress_Start_Date__c = System.today();
		op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
		op.Contract_Draft_Requested_Start_Date__c =  System.today();
		op.Award_Start_Date__c = System.today();
		op.TB_Contract_Signed_Start_Date__c = System.today();
        insert op;
		
        op.Number_of_Rounds_in_TruBid__c = 'Round 1';
        update op;
    }
    
    public static testmethod void test2()
    {
		Account acc= new Account();
		acc.name ='test';
		// add all required field here
		insert acc;
		
        Opportunity op = New Opportunity ();
		op.name ='TestOpportunity';
		op.CloseDate = system.today();
		op.StageName = 'Prospecting' ;
		op.accountid = acc.id;
		op.TB_Edits_in_Progress_Start_Date__c = System.today();
		op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
		op.Contract_Draft_Requested_Start_Date__c =  System.today();
		op.Award_Start_Date__c = System.today();
		op.TB_Contract_Signed_Start_Date__c = System.today();
        insert op;

        op.Number_of_Rounds_in_TruBid__c = 'Round 2';
        update op;
    }
    
    public static testmethod void test3()
    {
		Account acc= new Account();
		acc.name ='test';
		// add all required field here
		insert acc;
		
        Opportunity op = New Opportunity ();
		op.name ='TestOpportunity';
		op.CloseDate = system.today();
		op.StageName = 'Prospecting' ;
		op.accountid = acc.id;
		op.TB_Edits_in_Progress_Start_Date__c = System.today();
		op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
		op.Contract_Draft_Requested_Start_Date__c =  System.today();
		op.Award_Start_Date__c = System.today();
		op.TB_Contract_Signed_Start_Date__c = System.today();
        insert op;

        op.Number_of_Rounds_in_TruBid__c = 'Round 3';
        update op;
    }
}

 
This was selected as the best answer
CaisaCaisa
Amit - I see that the error is being cause because the Trigger is throwing a Null Pointer expection.

Can you please help me de-bug it? 

Here is the code which is causing Null Pointer: https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=906F0000000QtwsIAC
CaisaCaisa
Amit - the test now passes - BUT the code coverage is 0%

What am I doing incorrectly?
Amit Chaudhary 8Amit Chaudhary 8
Can you please post your final Trigger and Test class
CaisaCaisa
Final Trigger:

/* This Trigger is to calculate the Dates in the TruBid Completion
section based on the Number of TruBid Rounds the User has selected.
I've created a method to calculate the Business Days and called
the method to calculate the Dates */
 
 
trigger TruBidCompletion on Opportunity (before update)
{
public static Boolean IsWeekendDay(Date dateParam)
   {
      boolean result     = false;
      system.debug('dateParam = '+dateParam);
      //Recover the day of the week
      Date startOfWeek   = dateParam.toStartOfWeek();
      system.debug('startOfWeek = '+startOfWeek);
      Integer dayOfWeek  = dateParam.day() - startOfWeek.day();
      system.debug('dayOfWeek = '+dayOfWeek);  
      result = dayOfWeek == 0 || dayOfWeek == 6 ? true : false;
      system.debug('result = '+result);
      return result;
   }
  
  
   public static Date AddBusinessDays(Date StartDate, integer BusinessDaysToAdd )
   {
      //Add or decrease in BusinessDaysToAdd days
      Date finalDate = StartDate;
      system.debug('finaldate = '+finalDate);
      integer direction = BusinessDaysToAdd < 0 ? -1 : 1;
      system.debug('direction = '+direction);
       while(BusinessDaysToAdd != 0)
       {
           finalDate = finalDate.AddDays(direction);
           system.debug('BusinessDaysToAdd = '+BusinessDaysToAdd);           
           system.debug('finaldate = '+finalDate);
           if (!isWeekendDay(finalDate))
           {
               BusinessDaysToAdd -= direction;            
           }
       }
       return finalDate;
   }
 {
  for(Opportunity o : Trigger.new)
    {
    /* If the user selects Round 1 as the Number of TruBid Rounds */
    if(o.Number_of_Rounds_in_TruBid__c=='Round 1')
    {
    /* Call the AddBusinessDays method called above to add 'x' days */
    o.Contract_Draft_Requested_Start_Date__c=o.Rd_1_Results_Review_End_Date__c;
    o.Contract_Draft_Requested_End_Date__c=AddBusinessDays( o.Contract_Draft_Requested_Start_Date__c,1);
    o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
    o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
    o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
    o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
    o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
    o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
    o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
    o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
    }
  
   
    else if
    /* If the user selects Round 2 as the Number of TruBid Rounds */
    (o.Number_of_Rounds_in_TruBid__c=='Round 2')
    {
     /* Call the AddBusinessDays method called above to add 'x' days */
    o.Contract_Draft_Requested_Start_Date__c=o.Rd_2_Results_Review_End_Date__c;
    o.Contract_Draft_Requested_End_Date__c=AddBusinessDays(o.Contract_Draft_Requested_Start_Date__c,1);
    o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
    o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
    o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
    o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
    o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
    o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
    o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
    o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
   }
  
   else if
   /* If the user selects Round 3 as the Number of TruBid Rounds */
    (o.Number_of_Rounds_in_TruBid__c=='Round 3')
    {
     /* Call the AddBusinessDays method called above to add 'x' days */
    o.Contract_Draft_Requested_Start_Date__c=o.Rd_3_Results_Review_End_Date__c;
    o.Contract_Draft_Requested_End_Date__c=AddBusinessDays(o.Contract_Draft_Requested_Start_Date__c,1);
    o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
    o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
    o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
    o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
    o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
    o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
    o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
    o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
   }
  
}
  
 } 
}
 
 
 



 
CaisaCaisa
Final Test Class

Test Class
@isTest
 
public class Test_TruBidCompletion {
 
    
    public static testmethod void test1()
 
    {
 
        Account acc= new Account();
 
        acc.name ='test';
 
        // add all required field here
 
        insert acc;
 
        
 
        Opportunity op = New Opportunity ();
 
        op.name ='TestOpportunity';
 
        op.CloseDate = system.today();
 
        op.StageName = 'Prospecting' ;
 
        op.accountid = acc.id;
 
        op.TB_Edits_in_Progress_Start_Date__c = System.today();
 
        op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
 
        op.Contract_Draft_Requested_Start_Date__c =  System.today();
 
        op.Award_Start_Date__c = System.today();
 
        op.TB_Contract_Signed_Start_Date__c = System.today();
 
        insert op;
 
        
 
        op.Number_of_Rounds_in_TruBid__c = 'Round 1';
 
        update op;
 
    }
 
    
 
    public static testmethod void test2()
 
    {
 
        Account acc= new Account();
 
        acc.name ='test';
 
        // add all required field here
 
        insert acc;
 
        
 
        Opportunity op = New Opportunity ();
 
        op.name ='TestOpportunity';
 
        op.CloseDate = system.today();
 
        op.StageName = 'Prospecting' ;
 
        op.accountid = acc.id;
 
        op.TB_Edits_in_Progress_Start_Date__c = System.today();
 
        op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
 
        op.Contract_Draft_Requested_Start_Date__c =  System.today();
 
        op.Award_Start_Date__c = System.today();
 
        op.TB_Contract_Signed_Start_Date__c = System.today();
 
        insert op;
 
 
 
        op.Number_of_Rounds_in_TruBid__c = 'Round 2';
 
        update op;
 
    }
 
    
 
    public static testmethod void test3()
 
    {
 
        Account acc= new Account();
 
        acc.name ='test';
 
        // add all required field here
 
        insert acc;
 
        
 
        Opportunity op = New Opportunity ();
 
        op.name ='TestOpportunity';
 
        op.CloseDate = system.today();
 
       op.StageName = 'Prospecting' ;
 
        op.accountid = acc.id;
 
        op.TB_Edits_in_Progress_Start_Date__c = System.today();
 
        op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
 
        op.Contract_Draft_Requested_Start_Date__c =  System.today();
 
        op.Award_Start_Date__c = System.today();
 
        op.TB_Contract_Signed_Start_Date__c = System.today();
 
        insert op;
 
 
 
        op.Number_of_Rounds_in_TruBid__c = 'Round 3';
 
        update op;
 
    }
 
}
 
 
 
 
 
 
CaisaCaisa
Amit - it is working now. It hits 75% of the code. Thanks again for all your help!!