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
John McAfeeJohn McAfee 

Better test Coverage for invokable Method

Hi all I have a test class for this invokable method but only getting 31% coverage.. Any help would be appreciated to increase coverage.. been banging my head on it all weekend.. Thanks in advance... 

---Test---
@isTest
public class DefaultEntitlementTest {
    static testmethod void ValidateDefaultEntitlementAction() {
        Account[] acc = new Account[] {
            new Account(Name='Test Account 1'), 
            new Account(Name='Test Account 2')
        };
        insert acc;
        
        Contact[] contacts = new Contact[] {
            new Contact(FirstName='Test 1', LastName='McTesty', AccountId = acc[0].id),
            new Contact(FirstName='Test 2', LastName='McTesty', AccountId = acc[0].id)
        };
        insert contacts;
        
        Id entitlementProcessId = [SELECT Id FROM SlaProcess WHERE SObjectType = 'Case' LIMIT 1].Id;
        
        Entitlement[] ent = new Entitlement[] {
            new Entitlement (AccountId=acc[0].Id, SLAProcessId = entitlementProcessId, Name = 'Test Entitlement Account',
                                           StartDate = system.today().addDays(-1), EndDate = system.today().addDays(5)),
            new Entitlement (AccountId=acc[1].id, SLAProcessId = entitlementProcessId, Name = 'Test Entitlement Contact',
                                           StartDate = system.today().addDays(-1), EndDate = system.today().addDays(5))
        };
        insert ent;
        
        EntitlementContact ec = new EntitlementContact(ContactId = contacts[0].id, EntitlementId = ent[1].id);
        insert ec;
        
        
        Case c = new Case(AccountId = acc[0].id, ContactId = contacts[0].id, Subject='Test');
        insert c;
        
        c = [SELECT Id, EntitlementId FROM Case WHERE Id =: c.id LIMIT 1];
        system.assertEquals(ent[1].id, c.EntitlementId);
        
        c = new Case(AccountId = acc[0].id, ContactId = contacts[1].id, Subject='Test');
        insert c;
        
        c = [SELECT Id, EntitlementId FROM Case WHERE Id =: c.id LIMIT 1];
        system.assertEquals(ent[0].id, c.EntitlementId);
    }
}


---Method---

public class DefaultEntitlementAction {
    @InvocableMethod(label='Update Default Entitlements' description='Determines the default Entitlement for the Case and returns the updated Cases.')
    public static void updateDefaultEntitlements(List<Case> casesToUpdate) {
        List<Case> cases = [
            SELECT AccountId, EntitlementId, AssetId 
            FROM Case 
            WHERE Id IN:new Map<Id, Case>(casesToUpdate).keySet()
        ];
        
        Set<Id> accountIds = new Set<Id>();
            
        for (Case c : cases){
            if (c.EntitlementId == null && c.AccountId != null) {
                accountIds.add(c.AccountId);
            }
        }

        if(accountIds.size() > 0) {    
            List <Entitlement> entitlements = [
                SELECT StartDate, EndDate, AccountId, AssetId
                FROM Entitlement
                WHERE AccountId in :accountIds 
                    AND StartDate <= TODAY 
                    AND EndDate >= TODAY
            ];
            
            if(entitlements.size() > 0) {       
                Map<Id, Entitlement> entitlementsByAccountId = new Map<Id, Entitlement>();
                
                for(Entitlement e : entitlements) {
                    entitlementsByAccountId.put(e.AccountId, e);
                }
                
                for(Case c : cases){
                    if(c.EntitlementId == null && c.AccountId != null) {
                        Entitlement e = entitlementsByAccountId.get(c.AccountId);
                        
                        if(e != null) {
                            c.EntitlementId = e.Id;
                            
                            if(c.AssetId == null && e.AssetId != null) {
                                c.AssetId = e.AssetId;
                            }
                        }
                    }
                }
                
                update cases;
            }
        }
    }
}
Maharajan CMaharajan C
Hi John,

Please try the below test class:

@isTest
public class DefaultEntitlementTest {
     static testmethod void ValidateDefaultEntitlementAction() {
        Account[] acc = new Account[] {
            new Account(Name='Test Account 1'), 
            new Account(Name='Test Account 2')
        };
        insert acc;
        
        Contact[] contacts = new Contact[] {
            new Contact(FirstName='Test 1', LastName='McTesty', AccountId = acc[0].id),
            new Contact(FirstName='Test 2', LastName='McTesty', AccountId = acc[0].id)
        };
        insert contacts;
        
        Id entitlementProcessId = [SELECT Id FROM SlaProcess WHERE SObjectType = 'Case' LIMIT 1].Id;
        
        Entitlement[] ent = new Entitlement[] {
            new Entitlement (AccountId=acc[0].Id, SLAProcessId = entitlementProcessId, Name = 'Test Entitlement Account',
                                           StartDate = system.today(), EndDate = system.today()),
            new Entitlement (AccountId=acc[1].id, SLAProcessId = entitlementProcessId, Name = 'Test Entitlement Contact',
                                           StartDate = system.today().addDays(-1), EndDate = system.today().addDays(5))
        };
        insert ent;
        
        EntitlementContact ec = new EntitlementContact(ContactId = contacts[0].id, EntitlementId = ent[1].id);
        insert ec;
        
        Case c = new Case(AccountId = acc[0].id, ContactId = contacts[0].id, Subject='Test');
        insert c;
         
        List<Case> caseList = new List<Case>{c};
        
        DefaultEntitlementAction.updateDefaultEntitlements(caseList);
    }
}


Thanks,
Maharajan.C
John McAfeeJohn McAfee
Thanks very much..  I saved and ran the test then went back to my class and it is still sitting at 31%..  does it take time to update the percentage ?  also for some context, When I had this action as a trigger, my test would cover 93%..  so this is a little weird to me that it dropped to 31% by putting in in a calls..   Thanks again for your help..  
Maharajan CMaharajan C
Am getting 95% Coverage John.

Once you have ran the test class. Please refresh the Apex Class page also.


User-added image


Thanks,
Maharajan.C
John McAfeeJohn McAfee
Thanks very much. I will try to refreh.. You have been a grat help.
John McAfeeJohn McAfee
New Error when running this test class. "Methods defined as TestMethod do not support Web service callouts" this is very weird because it worked fine in sandbox and gave good code coverage.. I deployed to production and all worked.. Now I am coming back to add one field update to the entitlement class which works great, but now when I run the test again so I can redeploy, I get this error. I removed my added field and it still fails on the original. Any thoughts on why this test class is now throwing this error when it worked perfect 2 weeks ago would be appreciated.. the only change I can see is that I did a refresh of the sandbox from prod. Thanks in advance