I have created a schedulable apex that gets the leads that have been last modified for more than 3 days and perform an operation. But for my test class, i want to insert some test data... But the test data will have lastmodified date always today. So the query when i run the test class will not return results. How could i implement a solution to insert those test data and make the last modified date more than 3 so that query includes those records and pass the test ?


There are multiple ways.

  • Quick and dirty: do an if on the query and return today's records if is running test, return 3 days ago otherwise. (Be careful with timezone differences, and tests running at around mid-night)
  • Using Test.loadData()
  • Using JSON.deserialize()

some more guided steps are given on this post

I personally like option 2, as it allows you to easily maintain Test data and be able to have a quick look at it, but I understand that playing with recordTypes might be a challenge.


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.