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
Jonathan SpinkJonathan Spink 

Are you getting random Apex Test failures?

We have been getting Apex Test failures which have little connection to recent development. Sometimes they go away without any direct intervention. Also, it's often only one sandbox that complains.

I have managed to resolve one and the cause was records being returned from a SELECT statement in a different order from usual. So it seems that if you don't use 'ORDER BY' you can't rely on the order of the records being returned.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jonathan,

If you are using SeeAllData=true in test class then the test class will run based on the data present in the sandbox  So it way fail based on the data in the sandbox if it is not satisfying any condition.

And also you mentioned about select statement . Yes the Select statment may not return the same result in order always. You have to use Order by class for sorting of the result in an order.

If this solution helps, Please mark it as best answer.

Thanks,
 
Jonathan SpinkJonathan Spink
Thanks Praveen. We are also getting test failures where the records are sorted with 'ORDER BY CreatedDate'. I think this is because in a test, the records can be created in a short period of time and so may all have the same CreatedDate, even down to the millisecond.