1

I am trying to build a test class for my new trigger and part of the trigger is to check current time against the org business hours then set it as a boolean yes/no.

Code:

DateTime now =system.Now();
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Boolean isWithin= BusinessHours.isWithin(bh.id, now);

How would I write this as a test class to test if it is or is not within the org business hours?

1 Answer 1

1

This is a case where you will need to modify the code under test to test it. You will need a class if your trigger logic isn't already in a class:

public class Triggers {

    @TestVisible
    private static DateTime nowOverride;

    public static DateTime now() {
        return nowOverride == null ? System.now() : nowOverride;

    }
}

then your trigger gets its "now" from that class:

DateTime now = Triggers.now();
BusinessHours bh = ...;
...

then in your various test cases you can inject your own "now":

@IsTest
static void insideBusinessHours() {

    Triggers.nowOverride = DateTime.newInstance 2019, 9, 20, 10, 0, 0);

    // Get trigger to run
    // Asset results
}
6
  • Thanks for the response! So I need to adjust the Trigger then since that is all I have currently at the start after the opening Trigger TriggerName on Case (before insert) to seperate it into its own Class file itself or just create a method within the trigger to handle the Business hours? Commented Sep 20, 2019 at 22:01
  • @CraigPhoenix Yes, you can create a class that is shared by one or more triggers (e.g. Triggers in my answer) or move the trigger logic out into a separate class and merge in the code I posted. Personally, I'd start with the former solution.
    – Keith C
    Commented Sep 20, 2019 at 22:04
  • Thanks Keith. -- So create the public class Triggers you pasted first as a new class which the purpose of that is to capture the time now and allow override if I am understanding it correctly. -- then update my current trigger line 2 (first line on my code above) with the second code you shared then in my test class I can run the check in your third code segment? -- Just wanting to be sure I understand it all.Sorry if sounding dumb on this, I completed the Apex and dev basics courses but now I am branching into new areas and trying to learn as much as I can :) Commented Sep 20, 2019 at 22:13
  • @CraigPhoenix Yep that is what I suggest. Then write test methods with various times of day set to confirm the behavior of your trigger.
    – Keith C
    Commented Sep 20, 2019 at 22:14
  • So if I did Triggers.nowOverride = DateTime.newInstance 2019,20,10,0,0); like above would I then enter the normal system.now and set that it should return false? I guess that is the part now that I am confused on is how I would get the above to test against the trigger. Basically I have this IF following that uses the isWithin variable if(isWithin){ Return; }else{ List<Case> numberofcases = [SELECT ID FROM CASE where CaseAge__c<=:1]; How would I do the test with the noOverride function? Thanks again for your assist here! Commented Sep 20, 2019 at 22:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .