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
Manuel EcheverriaManuel Echeverria 

How to Alter or set system date while testing a trigger

I need to alter or set the system.date to another date for testing purposes (test the trigger), but I can't find a way to do it.

The trigger works great but it take the system.now().date as a value and the Test coverage always fails because I can not test all the cases:  

Example

   
    Datetime required = system.now().date();    
    Datetime validDate = system.now().date();     
    String todayDay= validDate.formatGMT('EEEE');  

   for ( Order__c Order : Trigger.new )
    {
        Order__c queryOrder= [SELECT Id,Name,Otter_FFA_Is_Updatable__c,Required_Date__c From Order__c where Id=:Order.Id];
        //if was no update through QAD
        if ((queryOrder.Otter_FFA_Is_Updatable__c=='')||(queryOrder.Otter_FFA_Is_Updatable__c==null))
        {//if Required_Date__c != null
        if ( Order.Required_Date__c != null )
        {
        required = Order.Required_Date__c;
            if(validDate.date().daysBetween(required.date())<=1){//If there is a difference of one day 
            if (todayDay.toUpperCase() =='THURSDAY'){
                if (hour<11){days=1;}
                else {days=4;}
            }
            else if (todayDay.toUpperCase() =='FRIDAY'){
                if (hour<11){days=3;}
                else {days=4;}
            }
            else if (todayDay.toUpperCase() =='SATURDAY'){
              {days=3;}
            }
            else if (todayDay.toUpperCase() =='SUNDAY'){
              {days=2;}
            }                
            else {
                if (hour<11){days=1;}
                else {days=2;}
            }

                
Those Thursday, friday, saturday and sunday I can test it.

Any advice will be appreciate it.
Best Answer chosen by Manuel Echeverria
Sunil02KumarSunil02Kumar
Hi Manuel,

Create a class and specify static variable. For example:

public class testHelper{
    public static DateTime getRequiredDate(){
        //specify what ever date time you want to pass to trigger for testing
        return system.now().adddays(-5);
    }
    public static DateTime getValidDate(){
        //specify what ever date time you want to pass to trigger for testing
        return system.now().adddays(-15);
    }
}

Now do small changes in trigger and replace first 2 lines with below code

Datetime required;
Datetime validDate
if(Test.IsrunningTest){
    required=testHelper.getRequiredDate();
    validDate=testHelper.getValidDate();
}else{
    required = system.now().date();    
    validDate = system.now().date();
}

In order to test, just change value in testHelper class and then run test class. 
Also in test class you can modify these static Datetime variables to pass required datetime values and you will get code coverage.

Hope this will help you.
[If it solves your problem, please mark it as solution]

Thanks,
Sunil Kumar
 

All Answers

Sunil02KumarSunil02Kumar
Hi Manuel,

Create a class and specify static variable. For example:

public class testHelper{
    public static DateTime getRequiredDate(){
        //specify what ever date time you want to pass to trigger for testing
        return system.now().adddays(-5);
    }
    public static DateTime getValidDate(){
        //specify what ever date time you want to pass to trigger for testing
        return system.now().adddays(-15);
    }
}

Now do small changes in trigger and replace first 2 lines with below code

Datetime required;
Datetime validDate
if(Test.IsrunningTest){
    required=testHelper.getRequiredDate();
    validDate=testHelper.getValidDate();
}else{
    required = system.now().date();    
    validDate = system.now().date();
}

In order to test, just change value in testHelper class and then run test class. 
Also in test class you can modify these static Datetime variables to pass required datetime values and you will get code coverage.

Hope this will help you.
[If it solves your problem, please mark it as solution]

Thanks,
Sunil Kumar
 
This was selected as the best answer
Manuel EcheverriaManuel Echeverria
Excellent Answer Kumar_sunil

I just followed your steps and its works.

Very simple and really usefull.

Thanks a lot...!!!