What specialized testing methods are available in the Test class for verifying asynchronous job queues?
Answer
Basics: The Test class has a small set of async-related helpers. Most async verification is done by running the job inside the start/stop block and asserting the outcome.
How it works:
Test.startTest()andTest.stopTest(): async jobs run synchronously atstopTest().Test.enqueueBatchJobs(Integer n): adds n dummy batch jobs to the Flex Queue so you can test queue logic.Test.getFlexQueueOrder(): returns the Flex Queue job Ids in order.Test.isRunningTest(): guard for logic that must not run in tests (such as chaining).Test.setMock(...): required for callouts in async code.
Advanced / deep dive: Real queue ordering cannot be exercised with real batch jobs in a test, since only one execute() runs and jobs complete at stopTest(). Hence enqueueBatchJobs creates placeholders to test code that calls FlexQueue.moveJobToFront and verify via getFlexQueueOrder(). Chained queueable jobs are not executed beyond the first in tests, so assert the first job and unit-test later stages by calling execute() directly with a mock context. Scheduled jobs run once at stopTest() regardless of their cron time. Verify status with AsyncApexJob queries after stopTest().
Code / config example:
@IsTest
static void testFlexQueueReorder() {
Test.startTest();
Test.enqueueBatchJobs(3); // three placeholder Holding jobs
List<Id> order = Test.getFlexQueueOrder();
System.assertEquals(3, order.size());
Boolean moved = FlexQueue.moveJobToFront(order[2]);
System.assert(moved);
List<Id> newOrder = Test.getFlexQueueOrder();
System.assertEquals(order[], newOrder[]);
Test.stopTest();
}