So I have a trigger and a schedule class. The trigger will fire upon update on an object and schedule a job which will execute the scheduled class. There is some logic in the trigger related to non-working hours, weekends and holidays. It will usually schedule the job 1 hour after insert (due to business requirement). but if insert happens over the weekend, afterhours, or holidays. It will schedule the next business day 8am.
My problem is my test class does not cover everything. I insert the object in test class, and depends on what time I run the test, it will cover different part. Like if I run during business hours, the weekend, holiday logic doesn't get covered, etc.
My question is there anyway in test class we can set the Datetime.Now to any date and time we want? thanks very much
Best Answer
A solution to this is as follows:
Then your trigger needs to always get the current time by using
Util.now
. In PROD, it will always return the true "now" time.But your testmethods can call
Util.now = testTime;
before doing DML on the triggered SObject. Your trigger code will then get a fake datetime so you can verify that your CronTrigger is properly setup.