function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Long Nguyen 19Long Nguyen 19 

Create Data for AsyncApexJob for testing

Hi Experts,

I want to test my controller which has below query:
List<AsyncApexJob> lstApexJob = [SELECT ApexClassId,status FROM AsyncApexJob where ApexClassId in:mapIDs.keySet()and (completedDate=yesterday or completedDate=today) ];
Because there's no Job which is completed on Yesterday or Today so the List is empty and my test coverage cannot reach target percent. So I tried to create a AsyncApexJob data for testing but system said that this object is not writeable:
AsyncApexJob aaj = new AsyncApexJob(completedDate=Date.today());
insert aaj;
Please show me the way to fix this issue.
 
bob_buzzardbob_buzzard
This is one of those areas where the Salesforce platform makes it difficult or impossible to test your exact production code. You can't create test data that is perfectly representative of the past in the database, so you have a couple of choices:

(1) Open up the test to @seealldata=true and hope there is some data that your test can piggy back on. I only mention this to draw attention to the fact that its a terrible idea :)
(2) Change the query that generates the list when executing a test, so that it returns an in-memory list of previously created test records.  I can't say how well this would work for your use case as it depends on what you are doing with the results.

There may be other options too, but option (2) above is the one that I typically use when I can.
lavanya g 9lavanya g 9
HI bob_buzzard,
could you please help me out how to change the query to generate list for testing the list what I created.