You need to sign in to do that
Don't have an account?
sfadm sfadm
Business Hours test code coverage
I have an apex source code where I have plenty of if/else conditions which depend on the business hours Mon - Fri 9h - 18h settings in Salesforce.
I'm creating a test method to validate my code but because of the business hours settings the apex code enters only in few if conditions.
And I cannot reach the necessary percentage to validate my code.
Is it possible in the test method to change the business hours so that I can validate my code on 100% or is there a way to validate my code despite the fact that it is depending on the business hours?
I'm creating a test method to validate my code but because of the business hours settings the apex code enters only in few if conditions.
And I cannot reach the necessary percentage to validate my code.
Is it possible in the test method to change the business hours so that I can validate my code on 100% or is there a way to validate my code despite the fact that it is depending on the business hours?
You can change the currentHour and startHourOfDay as you want using a block that is conditioned by the boolean Test.isRunningTest().
But that means a change of your tested class and not only your test class.
You build your tested class (the one used in production) with somes lines that will be executed only during the tests.
That means also a new deployment for this tested class of course in all the sandboxes and finally production.
You can use a custom setting object if you don't want to use a custom object.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm
Regards
All Answers
You can use the boolean Test.isRunningTest() in your tested class:
You can also force the created date just after the creations of objects in your test class ( Test.setCreatedDate( ... ) ) . But I am not sure at all that it is your problem without seeing your source code.
Best regards
Alain
Need a way to set the current hour from the test method.
Can you advise how this can be achieved?
You can change the currentHour and startHourOfDay as you want using a block that is conditioned by the boolean Test.isRunningTest().
But that means a change of your tested class and not only your test class.
You build your tested class (the one used in production) with somes lines that will be executed only during the tests.
That means also a new deployment for this tested class of course in all the sandboxes and finally production.
You can use a custom setting object if you don't want to use a custom object.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm
Regards