[SalesForce] How to set closeDate of a Case record in the test Method

I am writting a test method, where I have to set closedate of a case record to an old date.

I know, i can make Status to "closed", but it will give you close date as Today.

Is there any other way, where I can set the close date to an old date?

Best Answer

You should be able to do this, and set other system date fields, using JSON.deserialize(). For example (setting CreatedDate):

@isTest 
private class CaseTest{ 
static testmethod void testLoadData(){ 
String caseJSON = '{"attributes":{"type":"CasSe","url":"/services/data/v25.0/sobjects/Case/500E0000002nH2fIAE"},"Id":"500E0000002nH2fIAE","Status":"Open","CreatedDate":"2012-10-04T17:54:26.000+0000"}'; 
Case c = (Case) JSON.deserialize(caseJSON, Case.class ); 
System.debug('Test case:' + c.createdDate); 
System.debug('Test caseId:' + c.Id); 
System.debug('Test caseStatus:' + c.status); 

Case c1 = new Case(); 
c1.Id = c.Id; 
c1.status = 'New'; 
update c1; 

System.debug('Test caseStatus1:' + c1.status); 

} 
}

Above example and further detail here: https://help.salesforce.com/apex/HTViewSolution?id=000181873&language=en_US