Activity Forums Salesforce® Discussions Why we use Test.starTest and Test.stopTest in Salesforce?

  • Radhakrishna

    Member
    August 10, 2017 at 5:33 am

    Hello Shaharyar,

     

    As you must know by now that Apex is governed by certain boundaries. THe term coined for this is 'Governor Limits'.

    There are two additional system static methods provided by Apex. These methods, Test.startTest and Test.stopTest, are used when testing governor limits. Or in other words, executing test scenarios with a larger data set.

    These static methods allow a test method to separate the Apex resources and governor limits being used to prepare and initialize the dataset from the resources and limits used during the actual test execution.

    Bottom Line -

    Governor limits are reset when the Test.startTest appears and the code between Test.startTest and Test.stopTest executes in fresh set of governor limits (Context changes). Also Test.stopTest appears, the context is again moved back to the original code.

    Example -

    you have 100 lines of code in your test class.

    you have start test at 30
    you have stop test at 70

    So the line of code from 30 -70 is in different context of governor limits and
    line 1-30/71-100 in in different context.

    Total of 2 contexts here.

    Please mark this as best answer if your query is resolved.

     

  • shariq

    Member
    August 10, 2017 at 2:08 pm

    Hi Shaharyar,

    Test.startTest and Test.stopTest are used for asynchronous apex, like Batch Apex and Future calls. Calling method(s) between Test.startTest and Test.stopTest ensure that any asynchronous transactions finish executing before Test.stopTest() exits. This means, that if you use System.asserts after Test.stopTest, you'll have an accurate representation of your test. Otherwise, if you called System.assert before Test.stopTest, or if you didn't use Test.startTest / Test.stopTest, then you couldn't gaurantee that the batch or future transaction was complete.

    startTest()

    Marks the point in your test code when your test actually begins. Use this method when you are testing governor limits.

    Usage

    You can also use this method with stopTest to ensure that all asynchronous calls that come after the startTest method are run before doing any assertions or testing. Each test method is allowed to call this method only once. All of the code before this method should be used to initialize variables, populate data structures, and so on, allowing you to set up everything you need to run your test. Any code that executes after the call to startTest and before stopTest is assigned a new set of governor limits.

    stopTest()

    Marks the point in your test code when your test ends. Use this method in conjunction with the startTest method.

    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.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos