function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sgsg 

Test class coverage for a batch class which includes business hours

I have this batch class which included Busines hours check boolean method. I am having hard time including business hours check in test records and can't find a correct reference too. Can someone help me with a piece of code to include Business hours check logic in Test class? 

Below is the batch class
global class BatchCounter implements Database.Batchable<sObject>
{
static final String businessHoursId;
global Database.QueryLocator start(Database.BatchableContext bc)
{
String query = 'SELECT Id, numberField__c,statusField__c,Submitteddate__c FROM objectName__c WHERE Submitteddate__c !=null '+' AND statusField__c IN (\'Assigned\', \'Submitted\', \'InProgress\')'; return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<TMA__c> tmas)
{
for (TMA__c tma : tmas)
{
if (isBusinessHours(businessHoursId))
{
tma.numberField__c+=1;
}
}
update tmas;
}
global void finish(Database.BatchableContext bc)
{ }
private static Boolean isBusinessHours(Id businessHoursId)
{
BusinessHours businessHours = [SELECT Id FROM BusinessHours WHERE Id = :businessHoursId];
if (businessHours != null)
{
Datetime currentDatetime = Datetime.now();
return BusinessHours.isWithin(businessHoursId, currentDatetime);
}
return false;
} }
SubratSubrat (Salesforce Developers) 
Hello ,

To include the business hours check logic in your test class for the BatchCounter batch class, you can create test records with different scenarios and set the Submitteddate__c field accordingly. Here's an example of how you can modify your test class to cover the business hours check:
 
@IsTest
private class BatchCounterTest {
    @IsTest
    static void testBatchCounter() {
        // Create test Business Hours
        BusinessHours businessHours = new BusinessHours(
            Name = 'Test Business Hours',
            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
        );
        insert businessHours;

        // Create test records for BatchCounter
        List<TMA_c> tmas = new List<TMA_c>();
        for (Integer i = 0; i < 10; i++) {
            TMA_c tma = new TMA_c();
            tma.Submitteddate__c = DateTime.newInstanceGmt(2023, 5, 15, i, 0, 0);
            tma.statusField__c = 'Assigned'; // Assuming this is a valid status
            tmas.add(tma);
        }
        insert tmas;

        // Set the Business Hours ID in the BatchCounter
        BatchCounter.businessHoursId = businessHours.Id;

        // Instantiate and execute the BatchCounter
        Test.startTest();
        Database.executeBatch(new BatchCounter(), 5);
        Test.stopTest();

        // Verify the updated records
        List<TMA_c> updatedTmas = [SELECT numberFieldc FROM TMA_c];
        System.assertEquals(10, updatedTmas.size());
        
        for (TMA__c tma : updatedTmas) {
            if (BusinessHours.isWithin(businessHours.Id, System.now())) {
                System.assertEquals(1, tma.numberField__c);
            } else {
                System.assertEquals(0, tma.numberField__c);
            }
        }
    }
}

If it helps , please mark this as Best Answer.
Thank you.
sgsg
Hi Subrat, Thanks for your response. I tried this way, but DML operation is not allowed in Businesshours inside Test class.