+ Start a Discussion
sfdc18sfdc18 

test class

How to write test class for trigger which compare older and newer valuee
trigger GetAvailability on Case (before update) { 

Boolean oooFlag;
Set<ID> ids = Trigger.newMap.keyset();
ID oId;

List<Case> caseList = [Select Id, Status, OwnerId from Case where Id in : ids];

for(Case c : caseList) {
oId = c.OwnerId;
}

for (Case c : Trigger.New) {
    Case oldCase = Trigger.oldMap.get(c.Id);

    Boolean oldStatus = oldCase.Status.equals('Closed');

    Boolean newStatus = c.Status.equals('New');
 
    if(oldStatus && newStatus) {

      Map<Id, boolean> Map1 = GlobalClass.getAvailability(oId);

      for (Id id : Map1.keySet()) {  
      oooFlag = Map1.get(id);
      System.debug('===oooFlag ' + oooFlag);
      break;
      }
      if(!oooFlag){
      c.OwnerId = 'idtyt3RTGBhtu7u';
      }

    }
  }

}

 
PratikPratik (Salesforce Developers) 

Hi,

You can create a test case record/records and then update them with the values which you have mentioned in the code so the conditions will meet to cover the code. 

Thanks,
Pratik

sfdc18sfdc18
I did that still it is not covered
@isTest
private class MethodTest {
    
    
    static testMethod void testMethod() {

        Account   testAccount = new Account();
        testAccount.Name  = 'Test Account';
    
        insert testAccount;
    
        Contact   testContact = new Contact();
        testContact.LastName  = 'Test Name';
    
        insert testContact;

        Product2  testProduct = new Product2();
       
        testProduct.Name                  = 'Test Product Name';
        testProduct.Product_Category__c   = 'Category';
        testProduct.Product_Family__c     = 'Family';
        testProduct.Product_Sub_family__c = 'Sub-Family';    

        insert testProduct;
    
        Case  testCase  = new Case();
     
        testCase.Summary__c   = 'Summary';
        testCase.Description  = 'Description';
        testCase.Origin       = 'Email';
        testCase.Status       = 'Closed';
        testCase.ContactId    = testContact.Id;
        testCase.ProductId    = testProduct.Id;
        
        
        Test.startTest();
        insert testCase;
        
        testCase.Status = 'New';
        update testCase;
        
        Test.stopTest();
                       
    }
}