I am writing test class for two batch classes, X and Y.I executed batch X from one test method between test.starttest and test.stoptest.This works fine.


  • The issue is I'm inserting few records in X that I need in Y.

  • This is happening through two different buttons from user side.

  • Now if I execute both batch is same test.starttest and test.stoptest, i couldn't get the batch X done because its async process.

  • If I execute batch Y from other testmethod in test class, I'm unable to get the values inserted in batch X from previous test method.

How can I fix this issue?

  • 1
    Batch x will execute when your method hits test.stopTest(). Have you tried validating your data after that happens? – crmprogdev Feb 26 '16 at 19:30
  • Yes. Batch X will be finished at test.stoptest(). I cheked my data at that point. Its getting inserted. – Anurag Feb 26 '16 at 19:44

Short Answer:

Try calling both Batch X and Batch Y within the same testMethod and perform assert after stopTest.

Long Answer:

Here you are testing two batches X and Y.


  • We cannot call Test.stopTest more than once in a Test class
  • Without the stopTest call, asynchronous jobs will not run

Current Testing methodology:

Test Data Generation -> Call Batch X -> Call Batch Y -> StopTest and Assert -> End the test

Recommended Testing methodology:

Batch X test

Test 1 Data Generation -> Call Batch X  -> StopTest and Assert -> End the test

Batch Y test

Test 2 Stub the resultant data of Batch X -> Call Batch Y  -> StopTest and Assert -> End the test


  • So by this you don't really rely on execution of Batch X, instead you have stubbed the data which would exactly mimic the effect of Batch X execution.

  • Introducing stubbed data makes your tests independent and also makes your input and output rigid.

  • In future if the Batch Z calls the Batch Y at end of its execution, you don't need to assert the every new possible flow again, since Batch Y specifies its own set of Pre-Conditions

  • You will be able to test individual components to the fullest granularity, and any breaking implementation changes in Batch X will not raise false assertion failures for Batch Y since both the tests are decoupled

Reference on Async Batch limitations:

The System.schedule method starts an asynchronous process. This means that when you test scheduled Apex, you must ensure that the scheduled job is finished before testing against the results. Use the Test methods startTest and stopTest around the System.schedule method to ensure it finishes before continuing your test. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously. If you don’t include the System.schedule method within the startTest and stopTest methods, the scheduled job executes at the end of your test method for Apex saved using Salesforce.com API version 25.0 and later, but not in earlier versions.


  • Thankyou kathik, my first batch is executed fine from test class. Issue is, i need date inserted in first batch for executong second batch. I am executing second batch from other test method. So am unable to get results inserted in first test method from batch. – Anurag Feb 26 '16 at 19:52
  • @AnuragA I updated the answer. Please let me know your thoughts – karthikselva Feb 26 '16 at 20:12

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.