0

I'm struggling to get coverage on my if/else statements. Here is my test class:

@isTest private class defaultEntitlementTriggerTest{

Static testmethod void testInsert() {
    test.StartTest();
    
    Datetime targetTime = System.now(); //not sure if these are necessary

    Boolean isWithin= BusinessHours.isWithin(bh.id, targetTime); //not sure 
    
    List<Case> lstCase = new List<Case>();
    for (Integer i = 1 ; i<=10 ;i++) {  
        lstCase.add(new Case(
            Subject ='Test'+i,
            Status = 'New',
            EntitlementId = '1234'));
    }   

    insert lstCase;

    Case testCase = lstCase.get(0);
    testCase.subject = 'Test subject';
    testCase.EntitlementId = '4567';
    testCase.Status = 'Working';
    update testCase;
                       
    test.stopTest();
    
    System.assertEquals('Working', testCase.Status);
    System.assertEquals(10, lstCase.size()) ;
    System.assertEquals('4567', testcase.EntitlementId);
}

}

Here is my trigger:

trigger defaultEntitlement on Case (before insert, before update) {

    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

    // Create Datetime on for now in the local timezone.
    Datetime targetTime = System.now();

    Boolean isWithin= BusinessHours.isWithin(bh.id, targetTime);
    
    Entitlement[] afterHours = [SELECT Id FROM Entitlement WHERE Account.Name='3Fusion' AND Name LIKE '%NON BUSINESS%'];
    Entitlement[] duringHours = [SELECT Id FROM Entitlement WHERE Account.Name='3Fusion' AND Name LIKE '%STANDARD BUSINESS%'];
    
If ((afterHours.size() > 0) && (duringHours.size() > 0)) {
    for(Case newCase : Trigger.new){                            //not covered from here down
        If ((newCase.status == 'New') && (Trigger.isInsert)){  
            If (isWithin == TRUE){ newCase.EntitlementId = duringHours[0].Id; }
            Else If (isWithin == FALSE) { newCase.EntitlementId = afterHours[0].Id; }
        } 
        Else If ((newcase.status == 'New') && (Trigger.IsUpdate)) {
            Integer dt1 = (system.today()).daysBetween(date.valueof(newCase.createddate));

            If(dt1 >= 0)  {
                If (isWithin == TRUE){ newCase.EntitlementId = duringHours[0].Id; }
                Else If (isWithin == FALSE) { newCase.EntitlementId = afterHours[0].Id; }
            }  
        }
    }
}

}

I feel like I'm going back and forth on the same help articles and trails and really am not sure what I'm missing. Any help would be much appreciated.

1 Answer 1

1

In general, you need to create a set of test cases that cover every code branch.

For your code as it currently exists, you should test the following Cases:

  • insert a new Case within hours
  • insert a new Case outside hours
  • update a new Case within hours
  • update a new Case outside hours
  • update a new Case that comes from the future

The first four should set an Entitlement.Id, the last should not.

If you want to be completely thorough, you could also test:

  • insert or update a Case that isn't New
  • insert or update a Case when Entitlement.size is 0

HOWEVER, you do understand that your code is checking if a Case was created in the future? If such a thing can't happen, you can simplify your code tremendously:

If ((afterHours.size() > 0) && (duringHours.size() > 0)) {
    for(Case newCase : Trigger.new){ 
        If ((newCase.status == 'New') {  
            If isWithin { newCase.EntitlementId = duringHours[0].Id; }
            Else { newCase.EntitlementId = afterHours[0].Id; }
        } 
    }
}

You can safely assume that BH.isWithin() will never return null, therefore you don't need to test for false.

1
  • I definitely don't need to predict cases. :) Long story short is email to case will not fire the before insert trigger and I'm attempting to determine if the case was created in the last day. I'm sure there are better (re: correct) ways to do this. On the testing, I do understand WHAT I need to test, I just don't understand the CODE that I need to make that work. This all new to me, if that's not already clear. I do appreciate your thoughtful reply. I'm hoping you can help me get that last bit of code going. Mar 12, 2021 at 23:33

You must log in to answer this question.

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