I have a batch process that closes cases after 30 days of inactivity. Is it possible to write test code that would create a case that is over 30 days old?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
Not without request modifiable audit fields (not recommended). Instead, make the "days older than" variable so it can be set by the test method:
Construct your instance, call Test.startTest, call Database.executeBatch, set daysOlderThan to -1, create a case, then call Test.stopTest to execute your batch.
Take a look at Unit testing code which has logic around the CreatedDate where SObjects loaded via
I assume the same applies to fields like LastModifiedDate that your logic may be based upon.