I am new to salesforce development and I am quite not sure on when to use the below two methods:
Test.startTest()
and Test.stopTest()
1) Do i need to surround all my insert, update,SOQL and DML statements with startTest and stopTest?
The documentation also says that
Marks the point in your test code when your test actually begins.
2) Does it mean all the System.assert
statements should be surrounded with startTest() and stopTest()?
3) Is it OK if I add Test.startTest()
at the start of my test method and Test.stopTest()
at the end of my test method?
Marks the point in your test code when your test actually begins. Use this method when you are testing governor limits.
4) Documentation says use this to test the governor limits. I thought all the callouts, SOQL and DML statements in test methods do not count towards the governor limit. What exactly is testing governor limits??
5) Another thing, Is it mandatory to use these methods? What happens if I don't use? The example on this page(https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm) does not use these methods and the tests still pass. I can think of only one scenario, where we are forced to use these methods is when testing any asynchronous code so that the processing is finished before asserting the data.
My apologies if this question does not make any sense but I am just trying to understand when to use these two methods?