Starting and stopping your tests

One of the unique features of the Salesforce1 platform is its governor limits. These limits are intended to keep you from writing inefficient code and, more importantly, ensure that no one org is using vast amounts of resources at the detriment of other org's. Having said that, they can often cause problems when writing tests. After all, if you're creating your own test data, and testing in bulk, you may very well run into the max DML or max query count limits. This is why Salesforce created the platform-unique startTest and stopTest methods. These methods do more than just delineating the portions of your test that exercise your code; they provide you with two invaluable services. First, calling startTest() will reset your governor limits and limit them to just the code between startTest() and stopTest(). This means that you are free to create your test data without the creation of that data causing you to hit a governor limit. Additionally, it means that when the test runs, the governor limits summary at the end of the log is a summary not of the test itself, but of your tested code. This is incredibly useful for identifying processes and methods that, for instance, sometimes hit the execution time limit.

Secondly, calling stopTest() forces all asynchronous code to complete before returning to the test. This is invaluable as you write asynchronous code, such as @future methods and queueable Apex. This means that stopTest() will force any asynchronous work to complete not at some point in the future, but right now so that you can continue to test. This allows you to follow the same pattern for testing regardless of the tested code's synchronicity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.248.162