You need to sign in to do that
Don't have an account?
MTBRider
Modifying LastModifiedDate for test classes
I have seen a couple posting on this from sometime ago, but have not seen any solutions. I have logic that sends emails to owners of Opportunties that have not updated them for 30 days based on the LastModifiedDate of the record. The code in my test class cannot trigger this condition because I am unable to insert a test record with a LastModifiedDate from 30 days ago. Anyone have a solution for this? Thanks.
MTBRider,
How are you handling the time-dependent event: through a time-based workflow action or do you have a scheduled apex code that periodically checks opportunities?
You may have to add a check into your actual code using Test.isRunningTest() to see if the code is executing in test mode and if so, not rely on the lastModifiedDate field, rather on something else that you can control through in the test code.
All Answers
MTBRider,
How are you handling the time-dependent event: through a time-based workflow action or do you have a scheduled apex code that periodically checks opportunities?
You may have to add a check into your actual code using Test.isRunningTest() to see if the code is executing in test mode and if so, not rely on the lastModifiedDate field, rather on something else that you can control through in the test code.
Thanks for your reply. It is batch Apex via the scheduler. Let me give your suggestion of using isRunningTest() and see if I get anywhere with that. Thanks again.
Ok, so if you use batch apex, my guess is that most likely the start() method runs a query and in the 'where' clause you're checking for the lastModifiedDate.
You can pass the query itself from the test class to your batch apex. The query would be modified not to rely on the lastmodifiedDate.
You can create opportunities with names set to 'mytestopty', and then have where clause: " where name = 'mytestopty' "
You'd then execute your batch apex from the test class. Pseudo-code is:
Check out Batch apex docs - scroll down to the 'Testing Batch Apex' section.
The key thing is - you won't be able to test the scenario that involves the lastModifiedDate in your unit tests, but you can execute the same code using different criteria
I could not do it in the Where clause because my batch class actually has a pretty generic query that pulls back a lot of opportunities. There are a few conditions that I look for in the batch and then do different things based on what conditions are met. I ended up just using the isRunningTest() method to meet this condition if running the test class like so:
That worked. Thanks again for suggestion.