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
Snehal Gaware 15Snehal Gaware 15 

Need help on test class for before insert and before update trigger

Hi,
I need help in writing a test class for the following trigger. 
trigger CaseTrigger on Case (before insert,before update) 
{
    if((Trigger.isBefore && Trigger.isUpdate)||(Trigger.isBefore && Trigger.isInsert))
    {
        
           /*
   If the Entitlement Name is not set then, check to see if the Contact on the Case has an active Entitlement
    and select the first one.  If not then check to see if the Account on the Case has an active Entitlement.
   */
   List<Id> contactIds = new List<Id>();
   List<Id> acctIds = new List<Id>();
   for (Case c: Trigger.new){
      if (c.EntitlementId == null && c.ContactId!= null && c.AccountId!= null){
         contactIds.add(c.ContactId);
         acctIds.add(c.AccountId);
      }
   }
   if(contactIds.isEmpty()==false || acctIds.isEmpty()==false){
      /* Added check for active entitlement */
      List <EntitlementContact> entlContacts = [Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId From EntitlementContact e
                                                Where e.ContactId in:contactIds
                                                And e.Entitlement.EndDate >= Today And e.Entitlement.StartDate <= Today];
      if(entlContacts.isEmpty()==false){
         for(Case c: Trigger.new){
            if(c.EntitlementId == null && c.ContactId!= null){
               for(EntitlementContact ec:entlContacts){
                  if(ec.ContactId==c.ContactId){
                     c.EntitlementId = ec.EntitlementId;
                     if(c.AssetId==null && ec.Entitlement.AssetId!=null)
                        c.AssetId=ec.Entitlement.AssetId;
                     break;
                  }
               } // end for
            }
         } // end for
      } else{
         List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate, e.AccountId, e.AssetId
                                     From Entitlement e
                                     Where e.AccountId in:acctIds And e.EndDate >= Today And e.StartDate <= Today];
         if(entls.isEmpty()==false){
            for(Case c: Trigger.new){
               if(c.EntitlementId == null && c.AccountId!= null){
                  for(Entitlement e:entls){
                     if(e.AccountId==c.AccountId){
                        c.EntitlementId = e.Id;
                        if(c.AssetId==null && e.AssetId!=null)
                           c.AssetId=e.AssetId;
                        break;
                     }
                  } // end for
               }
            } // end for
         }
      }
   } // end if(contactIds.isEmpty()==false)
        
    }
    
    
 // [Based on assignment group selection change the case owner] 
   
	if(Trigger.isBefore && Trigger.isUpdate)
    {
        System.debug('entered if condition');
		
        for(Case c : Trigger.new)
		{
            if (c.Assignment_Group__c != trigger.oldMap.get(c.Id).Assignment_Group__c){
            System.debug('Updating logic');
       	    List<Group> qid = [select Id from Group where Name = : c.Assignment_Group__c and Type = 'Queue'];
              for(Group g : qid)
                {
                    c.OwnerId = g.id;
                    System.debug('updated');
                }
            }}        
    }
    
     if(Trigger.isBefore && Trigger.isUpdate)
    {
      for(Case c : Trigger.new)
		{   
			if(c.Assignment_Group__c=='Tech Support'||c.Assignment_Group__c=='GD-IT'||c.Assignment_Group__c=='App-Support'||c.Assignment_Group__c=='GD-RM'||c.Assignment_Group__c=='GD-DB'||c.Assignment_Group__c=='Dev-Ops'||c.Assignment_Group__c=='App-Management'||c.Assignment_Group__c=='PDT-DS-Engg'||c.Assignment_Group__c=='PDT-US-Engg')
            {
                c.Group_Manager_Email__c = 'sgaware1@gmail.com'; c.Escalation_Level_2_Email__c ='sgaware1@gmail.com'; c.Escalation_Level_3_Email__c='sgaware1@gmail.com';
            }
        }
    }
    
}

 
Best Answer chosen by Snehal Gaware 15
Manvendra Chaturvedi 26Manvendra Chaturvedi 26
This should work ,

@isTest
public class caseTestClass {
    @isTest static void caseTriggerTest(){
        Account acc=new Account();
        acc.name = 'Acc Name';
        insert acc;
        
        Contact con= new Contact();
        con.lastname = 'con lName';
        con.AccountId= acc.Id;
        insert con;
        
        Entitlement ent = new Entitlement(Name='test2', AccountId=acc.Id, StartDate=Date.valueof(System.now().addDays(-10)), EndDate=Date.valueof(System.now().addYears(3)));
        insert ent;
        
        EntitlementContact ec = new EntitlementContact(EntitlementId=ent.Id, ContactId=con.Id);
        insert ec;
        
        case c=new case();
        c.AccountId = acc.Id;
        c.ContactId = con.Id;
        c.Assignment_Group__c = 'test';
        insert c;
        
        c.Assignment_Group__c = 'test1';
        update c;
    }
    
    @isTest static void caseTriggerTest1(){
        Account acc=new Account();
        acc.name = 'Acc Name';
        insert acc;
        
        Contact con= new Contact();
        con.lastname = 'con lName';
        con.AccountId= acc.Id;
        insert con;
        
        Entitlement ent = new Entitlement(Name='test2', AccountId=acc.Id, StartDate=Date.valueof(System.now().addDays(-10)), EndDate=Date.valueof(System.now().addYears(3)));
        insert ent;
                
        case c=new case();
        c.AccountId = acc.Id;
        c.ContactId = con.Id;
        insert c;
    }
}

All Answers

Manvendra Chaturvedi 26Manvendra Chaturvedi 26
This should work ,

@isTest
public class caseTestClass {
    @isTest static void caseTriggerTest(){
        Account acc=new Account();
        acc.name = 'Acc Name';
        insert acc;
        
        Contact con= new Contact();
        con.lastname = 'con lName';
        con.AccountId= acc.Id;
        insert con;
        
        Entitlement ent = new Entitlement(Name='test2', AccountId=acc.Id, StartDate=Date.valueof(System.now().addDays(-10)), EndDate=Date.valueof(System.now().addYears(3)));
        insert ent;
        
        EntitlementContact ec = new EntitlementContact(EntitlementId=ent.Id, ContactId=con.Id);
        insert ec;
        
        case c=new case();
        c.AccountId = acc.Id;
        c.ContactId = con.Id;
        c.Assignment_Group__c = 'test';
        insert c;
        
        c.Assignment_Group__c = 'test1';
        update c;
    }
    
    @isTest static void caseTriggerTest1(){
        Account acc=new Account();
        acc.name = 'Acc Name';
        insert acc;
        
        Contact con= new Contact();
        con.lastname = 'con lName';
        con.AccountId= acc.Id;
        insert con;
        
        Entitlement ent = new Entitlement(Name='test2', AccountId=acc.Id, StartDate=Date.valueof(System.now().addDays(-10)), EndDate=Date.valueof(System.now().addYears(3)));
        insert ent;
                
        case c=new case();
        c.AccountId = acc.Id;
        c.ContactId = con.Id;
        insert c;
    }
}
This was selected as the best answer
Snehal Gaware 15Snehal Gaware 15
Thanks Manvendra. It worked. Previously code coverage was 81 now it is 95.