+ Start a Discussion

BusinessHours and Test class

I am facing difficulties in writing test class. The main class logic reads the current date and time, reads the default business hours data, and performs functions based on the current date or time is within business hours or not. Also, it schedules jobs for end of business. My problem is since DML is not allowed in business hours, my coverage doesnt always remain constant and at times tests fail if i try to run it on weekends or holidays? Has anyone faced this before? Any suggestions to approach this problem would be very useful. 




there might be better solutions but this should work.


Let your class have a global variable of Type Boolean (lets call it 'test') and initialize it with 'false'.

Add a function to the class that sets 'test' to true when it's called.


At the stage your're reading the current time, add an if clause asking if 'test' is true or not.

If it's true you set a time that suits your needs and if it turned false (thats the standard) it gets the current time.