+ 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){

}