0

We have a very old Case trigger which has a condition for a particular record type if the Task is open then case close should not close the case, Now the business user want to lift that restriction, Hence I have commented the code to bypass it.

How ever when I try to test the code coverage I am getting exception error.

Trigger

  trigger CaseTrigger on Case (after update) {
   /* 
    The first part of the trigger will close all the activities related to a case that satisfy either one of the Scenarios below
        Scenario 1 - Case Type = Junk Email 
        Scenario 2 - Case Record Type = IS APAC CS - Fax & IsClosed = True 

    */  
    final ID IS_APAC_FAX_CASE_RECORDTYPEID = '01220000000jBwB';
    List<Case> cases = Trigger.new;
    Map<ID, Case> caseMap = new Map<ID,case>();

    //if the case satisy the condition, add them to a map
    for(Case c:cases)
    {
        if(c.Case_Subject__c == 'Junk Email' ){
            caseMap.put(c.id, c);
        } 
        if(c.recordtypeid == IS_APAC_FAX_CASE_RECORDTYPEID &&  (!c.isClosed) ){
            caseMap.put(c.id, c);
        }

    }
    //Use the case map to get the tasks related to it 
    List<Task> filteredTasks = [SELECT  id, WhatID, Status FROM Task where  Status  != 'Completed' and  
                                                whatid IN :  caseMap.keyset()];


    //Update the status of the tasks to completed
    for(Task task: filteredTasks){
        task.Status = 'Completed';
    }

    //do the actual update. 
    if(filteredTasks.size()>0){
        update filteredTasks;
    }


    /*
     The second part of the trigger ensures that a case can be closed only if all the activities linked to it are closed as well. 


    */
    // Find all cases that are being closed...
    Set<Id> newlyClosedCaseIds = new Set<Id>();
    for (Id caseId : Trigger.newMap.keySet()) {
        if (Trigger.newMap.get(caseId).IsClosed && 
            !Trigger.oldMap.get(caseId).IsClosed && 
            (Trigger.newMap.get(caseId).recordTypeId == '01220000000MLHQ' 

            )) {
            newlyClosedCaseIds.add(caseId);
        }
    }

    // For every newly closed Case that has at least one 
    // not closed task add an error to the Trigger.newMap entry.
    // Adding the error to the Trigger.new entry will prevent the save
    // and add the error message to the page messages that get displayed on
    // the page.
    /*for (AggregateResult aggResult : [
            Select Count(Id), WhatId
            From Task
            Where WhatId In :newlyClosedCaseIds
                  And IsClosed = false
            Group by WhatId
            Having Count(Id) > 0
    ]) {
        Id caseId = (Id) aggResult.get('WhatId');
        Case errorCase = Trigger.newMap.get(caseId);

        // change error message as appropriate...
        errorCase.addError('Cannot close case since there are open tasks associated that needs to be closed. ' ); 
    } */
}

Test Class:

 @isTest
private class CaseCloseTriggerTest
 {  
     static testMethod void testUpdate() 
    {
          Case c = new Case();   
          c.RecordtypeID='01220000000MLHQ'; //IS-APAC-Membership
          c.SuppliedName='Bhawna Sinha';//Web Name
          c.SuppliedEmail='bhawna.sinha@saiglobal.com';//Web Email
          c.Subject='Test Email to Case';
          c.Description='Test email to Case - Description' ;         
          c.Status='1. New'; //New Case
          insert c;

          Task t1 = new Task();
          t1.WhatID = c.ID;
          t1.OwnerID='00520000002ZEed';//Bhawna Sinha
          t1.Subject='Test Email to Case Subject';  
          t1.WhoID='0032000000Lbp4H';//Michael Hodges
          t1.Status='Not Started';
          insert t1;


          Task t2 = new Task();
          t2.WhatID = c.ID;
          t2.OwnerID='00520000002ZEed';//Bhawna Sinha
          t2.Subject='Test Email to Case Subject';  
          t2.WhoID='0032000000Lbp4H';//Michael Hodges
          t2.Status='Completed';
          insert t2;          



         Test.startTest();  
         boolean updateFailed = False;       
          try{
             c.Status='9.  Completed';   
             update c;              
              System.assertEquals( true, False );
                Case c2 = [SELECT Status, isClosed FROM Case WHERE Id = :c.Id];
                System.assertEquals( 'New', c2.Status );
                System.assertEquals( false, c2.isClosed );
             }
          catch(Exception e){ 
               System.assertEquals( true, true );              
         }


         Test.stopTest();

     }
 }

The error i am getting is

Error: System.AssertException: System.AssertException: Assertion Failed: Expected: true, Actual: false

Stack Trace:- Class.CaseCloseTriggerTest.testUpdate: line 39, column 1

1

assertEquals(expected, actual, msg)

Asserts that the first two arguments are the same. If they are not, a fatal error is returned that causes code execution to halt.

As I can see, You have checked at line no. 39 as follows:-

 System.assertEquals( true, False );

It will never be going to execute and a fatal error is returned as you have explained in your problem.

Why you are doing so, I not able to understand but I can say you that We generally use this for “Positive Case Testing” and “Negative Case Testing” with single and multiple records.

If You to have test something in your test class and for making yourself sure you should do assert equals as you have done in your code at line no. 41

System.assertEquals( 'New', c2.Status );

So, Remove the Line number 39 from code as it is of no importance. This will solve your problem.

Note:- You can’t catch an assertion failure using a try/catch block even though it is logged as an exception.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.