+ Start a Discussion
CMcCaulCMcCaul 

How to close case in trigger test

I have a case trigger that closes all open tasks when a case is closed that is working in my sandbox:
trigger CaseCloseTrigger on Case (after update) {
Case[] cse = Trigger.new;

// Get case id's for closed cases
Set<ID> cseIds = new Set<ID>();
for (Case c:cse)
  if (c.isClosed)
  cseIds.add(c.Id);

// Get tasks on cases being closed, update and close if still open
  for (Task t : [select isclosed, status from task where whatid in :cseIds for update])
  if (!t.isclosed){
    t.description = 'Case closed';
    t.status = 'Completed';
    update t;
  }
}

 I am trying to write a test method to in order to deploy the trigger (I'm using Eclipse).
 
public class caseTestTriggerMethod {
  static testMethod void myTest(){

  Account acc = new account(name = 'TestCoverage');
  insert acc;
  Contact con = new contact(accountId = acc.id, lastname='Coverage', firstname='Test');
  insert con;
  Case cseA = new case(accountId = acc.id, contactId = con.id, status = 'New', type = 'Help Request', origin = 'Email', subject = 'Test Coverage 1');
  insert cseA;
  Case cseB = new case(accountId = acc.id, contactId = con.id, status = 'Closed', type = 'Help Request', origin = 'Email', subject = 'Test Coverage 2');
  insert cseB;
  Task tskA_1 = new task(ownerId = '00530000000f6Qn', whatId = cseA.id, whoId = con.id, subject = 'Task A.1', status = 'Not Started', priority = 'Normal', reminderdatetime = null);
  insert tskA_1;
  Task tskA_2 = new task(ownerId = '00530000000f6Qn', whatId = cseA.id, whoId = con.id, subject = 'Task A.2', status = 'Completed', priority = 'Normal', reminderdatetime = null);
  insert tskA_2;
  Task tskB_1 = new task(ownerId = '00530000000f6Qn', whatId = cseB.id, whoId = con.id, subject = 'Task B.1', status = 'Not Started', priority = 'Normal', reminderdatetime = null);
  insert tskB_1;
  Task tskB_2 = new task(ownerId = '00530000000f6Qn', whatId = cseB.id, whoId = con.id, subject = 'Task B.2', status = 'Completed', priority = 'Normal', reminderdatetime = null);
  insert tskB_2;
 
  cseA.status = 'Closed';
  update cseA;

  System.assertEquals(cseA.status,'Closed');
  System.assert(cseA.isClosed);
 
  System.assertEquals(tskA_1.status,'Completed');
  System.assertEquals(tskA_2.status,'Completed');
  System.assertEquals(tskB_1.status,'Not Started');
  System.assertEquals(tskB_2.status,'Completed');
  System.assertEquals(tskA_1.description,'');
  System.assertEquals(tskA_2.description,'Case Closed');
  System.assertEquals(tskB_1.description,'');
  System.assertEquals(tskB_2.description,'');
  }
}

The cseA.status assertion is true, status is closed, but the cseA.isClosed fails and my task is not closed nor status changed to completed.  It's working fine in the sandbox, so I'm guessing that just changing the case status to 'Closed' is not enough to actually close the case.  Is there some method I need to call to get the case closed?
 
Thanks for any help!

Best Answer chosen by Admin (Salesforce Developers) 
Ron HessRon Hess
Note:
A well written test method will not hard code object ID's , remember these may be different in your production environment.

if you need an owner id in your test method, best advise is to query the system for this value, that way it has a good chance of working (tests pass) in multiple environments (DE or Sandbox and Production). You risk passing in Developer Edition and failing in production just when you are ready to deploy your code.

All Answers

CMcCaulCMcCaul
Nevermind.  Dumb mistake.  I was not processing the assertions against the updated data.
Ron HessRon Hess
Note:
A well written test method will not hard code object ID's , remember these may be different in your production environment.

if you need an owner id in your test method, best advise is to query the system for this value, that way it has a good chance of working (tests pass) in multiple environments (DE or Sandbox and Production). You risk passing in Developer Edition and failing in production just when you are ready to deploy your code.
This was selected as the best answer