[SalesForce] Set Business Hours in Apex Test Code

I have written a class to calculate business hours for Case age in business hours. The class works great my issue is when trying to write decent test code for the class.

In following testing best practices, I want to create all my own test data and not rely on existing data (seeAllDate=true). I would assume that in order to test my different scenarios I would need to create and insert new business hours. Normally, I would use something like the snippet below

BusinessHours newHours = new BusinessHours(
    MondayStartTime = Time.newInstance(9, 0, 0, 0),
    MondayEndTime = Time.newInstance(17, 0, 0, 0),
    TuesdayStartTime = Time.newInstance(9, 0, 0, 0),
    TuesdayEndTime = Time.newInstance(17, 0, 0, 0),
    WednesdayStartTime = Time.newInstance(9, 0, 0, 0),
    WednesdayEndTime = Time.newInstance(17, 0, 0, 0),
    ThursdayStartTime = Time.newInstance(9, 0, 0, 0),
    ThursdayEndTime = Time.newInstance(17, 0, 0, 0),
    FridayStartTime = Time.newInstance(9, 0, 0, 0),
    FridayEndTime = Time.newInstance(17, 0, 0, 0),
    IsActive = true,
    IsDefault = true
);
insert newHours;

However, you cannot perform DML on business hours. Is there a way to do this without having to use seeAllData=true?

I have searched and searched but can;t find much info on using the business hours class in test code. Any help is much appreciated.

Best Answer

You don't need to use @SeeAllData=true to be able to access the existing BusinessHours, as this is organization data rather than user data. This isn't that well documented - the concept is covered at :

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

but BusinessHours isn't in the list.

To confirm this I've created a simple test class in one of my dev orgs that has a bunch of accounts and a single BusinessHours record:

@IsTest
private class BHTest {
    static void TestBH()
    {
        List<BusinessHours> bhs=[select id from BusinessHours where IsDefault=true];
        System.assert(bhs.size()==1);
        List<Account> accs=[select id from Account];
        System.assert(accs.size()==0);
    }
}

Executing this test confirms that I can see the BusinessHours records, but I can't see any accounts as they are isolated from my test.

Unfortunately you still have to rely on the BusinessHours record being present in the Salesforce instance your tests are running in.