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
StaciStaci 

test class for entitlement trigger

I've got milestones comleting automatically through triggers and I'm trying to create a test class for code coverage (I'm not good at these).
Here's my trigger
trigger CW_CompleteFirstResponseTimeMilestone on Case (after update) {
if (UserInfo.getUserType() == 'Standard')
{
    DateTime completionDate = System.now();
        List<Id> updateCases = new List<Id>();
        For (Case c : Trigger.new)
        {
            if((c.Incident_First_Response__c != null) && (c.Impact__c == 'Site Production Down') && ((c.SlaStartDate<=completionDate) && (c.SlaExitDate==null)))
            {
            updateCases.add(c.Id);
            CW_MilestoneUtils.completeMilestone(updateCases, 'CW First Response - P1', completionDate);
            }else{
                if((c.Incident_First_Response__c != null) && (c.Impact__c == 'Site Production Severely Impaired') && ((c.SlaStartDate<=completionDate) && (c.SlaExitDate==null)))
                {
                updateCases.add(c.Id);
                CW_MilestoneUtils.completeMilestone(updateCases, 'CW First Response - P2', completionDate);
                }else{
                    if((c.Incident_First_Response__c != null) && (c.Impact__c == 'Site Production Moderately Impaired') && ((c.SlaStartDate<=completionDate) && (c.SlaExitDate==null)))
                    {
                    updateCases.add(c.Id);
                    CW_MilestoneUtils.completeMilestone(updateCases, 'CW First Response - P3', completionDate);
                    }else{
                        if((c.Incident_First_Response__c != null) && (c.Impact__c == 'Site Production Can Continue Without Interruption') && ((c.SlaStartDate<=completionDate) && (c.SlaExitDate==null)))
                        {
                        updateCases.add(c.Id);
                        CW_MilestoneUtils.completeMilestone(updateCases, 'CW First Response - P4', completionDate);
                        }
        }
    
        }}}
                
           }
}

Here's what I have for the test class so far.  It comes up covering nothing(like I said, I'm not good at these)
@isTest
private class CW_TestCompleteMilestoneCaseTest{

static testMethod void CW_TestCompleteMilestoneCaseTest(){
User u1;
User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];        
System.runAs (thisUser) {
        //create Tier 2 user
        u1 = CW_DataCreation.CW_u1DataCreation();
    
RecordType rtAcct = [select Id from RecordType WHERE Name = 'Worksite' and SobjectType = 'Account' limit 1];

test.startTest();
system.runAs(u1){
//create an account
Account a1 = new Account(Name='My Account', RecordType = rtAcct, Technology_Account__c = true);
insert a1;

    Entitlement ent = new Entitlement(Name='Testing', AccountId=a1.Id, 
    StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)));
    insert ent;
  
        Case c = new Case();
        c.Subject = 'Test Case with Entitlement';
        c.EntitlementId = ent.Id;
        c.AccountId = a1.Id;
        c.Description = 'Test';
        c.Product__c = 'Fleet'; 
        c.CW_Type__c = 'Product Support';
        c.CW_Case_Reason_Custom__c = 'Incident';
        c.Application__c = 'Underground';
        c.Subsystem__c = 'Infrastructure';
        c.Incident_Start__c = datetime.now();
        c.Impact__c = 'Site Production Down';
    insert c;
    
    //update First Response
	c.Incident_First_Response__c = datetime.now();
	update c;
    
    DateTime completionDate = System.now();
    List<Id> updateCases = new List<Id>();
    
      if((c.Incident_First_Response__c != null) && (c.Impact__c == 'Site Production Down') && ((c.SlaStartDate<=completionDate) && (c.SlaExitDate==null)))
     {    
      updateCases.add(c.Id);
    CW_MilestoneUtils.completeMilestone(updateCases, 'CW First Response - P1', completionDate);
     }//}
    
    
}}}
///from SF entitlement admin guide
static testMethod void testCompleteMilestoneViaCase(){
    Entitlement entl = [select id from Entitlement limit 1];
    String entlId;
    if (entl != null)
        entlId = entl.Id;
    List<Case> cases = new List<Case>{};
    for(Integer i = 0; i < 1; i++){
        Case c = new Case(Subject = 'Test Case ' + i);
        cases.add(c);
        if (entlId != null){
            c = new Case(Subject = 'Test Case with Entitlement ' + i,
            EntitlementId = entlId);
            cases.add(c);
        }
    }

    insert cases;

   
    
    
}
}

Any help would be greatly appreciated!
Rohit Sharma 66Rohit Sharma 66
HI,

First of all create a user with user type 'Standard', or u can query as well, then run the code in the context of that user, This might be the reason u are not getting any code coverage.
As per ur line no 2 of trigger, u come inside the loop only if ur userType is standard.
Then u only need to create the test data for case record according to ur if conditions in to the trigger.
U need not to check the if condition in the test class, u just need to pass the test data in the trigger, means u just need to insert the case record and update it, which auto fires the the triggers.

Just try and let me know if u get any issues.
StaciStaci
Thank you @Rohit Sharma 66

I'm starting to get coverage now.  I'm only at 50% though.  Not sure how to get these few lines covered, can you help with that?

Test Class
@isTest
private class CW_TestCompleteMilestoneCaseTest{

static testMethod void CW_TestCompleteMilestoneCaseTest(){
User u1;
User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];        
System.runAs (thisUser) {
        //create Tier 2 user
        u1 = CW_DataCreation.CW_u1DataCreation();
    
RecordType rtAcct = [select Id from RecordType WHERE Name = 'Worksite' and SobjectType = 'Account' limit 1];

test.startTest();
system.runAs(u1){
//create an account
Account a1 = new Account(Name='WESTRAC PTY. LTD. (T080)', RecordType = rtAcct, Technology_Account__c = true);
insert a1;

    Entitlement ent = new Entitlement(Name='Testing', AccountId=a1.Id, 
    StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)));
    insert ent;
  
        Case c = new Case();
        c.Subject = 'Test Case with Entitlement';
        c.EntitlementId = ent.Id;
        c.AccountId = a1.Id;
        c.Description = 'Test';
        c.Product__c = 'Fleet'; 
        c.CW_Type__c = 'Product Support';
        c.CW_Case_Reason_Custom__c = 'Incident';
        c.Application__c = 'Underground';
        c.Subsystem__c = 'Infrastructure';
        c.Incident_Start__c = datetime.now();
        c.Impact__c = 'Site Production Down';
    insert c;
    
    //update First Response
    DateTime dt = System.now();
    c.Incident_First_Response__c = dt.addMinutes(5);
    update c;
}}}
}

Trigger
trigger CW_CompleteFirstResponseTimeMilestone on Case (after update) {
//if (UserInfo.getUserType() == 'Standard')
//{
    DateTime completionDate = System.now();
        List<Id> updateCases = new List<Id>();
        For (Case c : Trigger.new)
        {
            if((c.Incident_First_Response__c != null) && (c.Impact__c == 'Site Production Down') && ((c.SlaStartDate<=completionDate) && (c.SlaExitDate==null)))
            {
            updateCases.add(c.Id);
            CW_MilestoneUtils.completeMilestone(updateCases, 'CW First Response - P1', completionDate);
            }else{
                if((c.Incident_First_Response__c != null) && (c.Impact__c == 'Site Production Severely Impaired') && ((c.SlaStartDate<=completionDate) && (c.SlaExitDate==null)))
                {
                updateCases.add(c.Id);
                CW_MilestoneUtils.completeMilestone(updateCases, 'CW First Response - P2', completionDate);
                }else{
                    if((c.Incident_First_Response__c != null) && (c.Impact__c == 'Site Production Moderately Impaired') && ((c.SlaStartDate<=completionDate) && (c.SlaExitDate==null)))
                    {
                    updateCases.add(c.Id);
                    CW_MilestoneUtils.completeMilestone(updateCases, 'CW First Response - P3', completionDate);
                    }else{
                        if((c.Incident_First_Response__c != null) && (c.Impact__c == 'Site Production Can Continue Without Interruption') && ((c.SlaStartDate<=completionDate) && (c.SlaExitDate==null)))
                        {
                        updateCases.add(c.Id);
                        CW_MilestoneUtils.completeMilestone(updateCases, 'CW First Response - P4', completionDate);
                        }
        }
    
        }}}
                
           //}
}

These are the lines that are not covered(P2, P3, P4)
updateCases.add(c.Id);
            CW_MilestoneUtils.completeMilestone(updateCases, 'CW First Response - P1', completionDate);