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
Renuka G 9Renuka G 9 

creating date dynamically in salesforce apex test class

Hi,
We have a scheduler class, which creates task on every Dec1. It checks for Date.Today().Day()==1 && Date.Today().Month()== 12.. Somebody please help me to how cover this.. 
Best Answer chosen by Renuka G 9
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Instead of testing directly with Date.Today(), Assign Date.Today() to a variable and hardcode the Date value when it is running under Testing Context.
Date todayDate;
if(!Test.isRunningTest) {
todayDate = Date.today();
} else {
todayDate = Date.newInstance(Date.today().year(), 12, 1);
}

Use "todayDate" variable to check the condition. Now Your Task creation code will execute in both ways.

Thanks
Satya. 

All Answers

V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Instead of testing directly with Date.Today(), Assign Date.Today() to a variable and hardcode the Date value when it is running under Testing Context.
Date todayDate;
if(!Test.isRunningTest) {
todayDate = Date.today();
} else {
todayDate = Date.newInstance(Date.today().year(), 12, 1);
}

Use "todayDate" variable to check the condition. Now Your Task creation code will execute in both ways.

Thanks
Satya. 
This was selected as the best answer
Renuka G 9Renuka G 9
Thank You Satya.. It worked :-)