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
Andrew Hoban 6Andrew Hoban 6 

Unable to cover System.Today() in a test class

Hi,

I have a trigger that uses system.today() in multiple if statements. 

I am having difficuly trying to cover this in my test class. One of the statements checks if the current month is either July or August. As im currently testing this in January, it will not be covered.

What is the best way to dynamically cover this scenario?

Thanks,

Trigger (Currnent month is Janurary)
if(System.today().Month() == 7 || System.today().Month() == 8){

}


 
Best Answer chosen by Andrew Hoban 6
Aleksei KosovAleksei Kosov
 in class
@TestVisible private Integer currentMonth = System.today().Month();

in test
yourClass.currentMonth = 7;

Maybe it helps you.
 

All Answers

Aleksei KosovAleksei Kosov
 in class
@TestVisible private Integer currentMonth = System.today().Month();

in test
yourClass.currentMonth = 7;

Maybe it helps you.
 
This was selected as the best answer
Aleksei KosovAleksei Kosov
also in class
if(currentMonth == 7 || currentMonth  == 8){

}