3

I need to run an apex job inside a test:

test.startTest();

Id jobid = Database.executeBatch(new ApexJob());

and then wait for the apex job to finish and then stop the test:

test.stopTest();
6

That's exactly what Test.stopTest() does. It makes sure all asynchronous processing has finished before continuing on with the rest of the logic in your testMethod.

static testMethod void testSomeAsyncProcess()
{
    Test.startTest();
        someAsyncProcess();
    Test.stopTest();
    // all asynchronous processing is now complete.
    // make your assertions here
}

From the Test.stopTest() docs:

Usage

Each test method is allowed to call this method only once. Any code that executes after the stopTest method is assigned the original limits that were in effect before startTest was called. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously.

0

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.