+ Start a Discussion

Why system.assert() method calling after test.stopTest()

Hi Guys,


As salesforce standard document says:

The Test.stopTest method marks the point in your test code when your test ends. Use this method in conjunction with the startTest method. Each test method is allowed to call this method only once. After calling this method, any post assertions are done in the original context.




I need one urgent help..Can any one tell me why do we use system.assert() method after  test.stopTest() ?


Please suggest me and also let me know If I getting things wrong.













When test ends, we verify the behavior of code with System.assert methods. For further details refer following documents:






Hi Vinita_SFDC,


Thanks for the quick reply.


Yes. You are correct that we write system.assert() to test functional behaviour. Can't we write system.assert() method before test.stopTest() that means inside test.startTest() and test.stopTest()?


Please suggest.


Best regards,



Hello Joo,


Between test.startTest() and test.stopTest() we get a fresh set of governor limits, here we test for governon limits. After test.stopTest() another execution context starts with new set governor limits.


So if you include your assert statements within test methods, these statements will be counted under apex script statement limit. That is why we write assert statement out of test methods.

Jonathan AndersonJonathan Anderson
Vinita_SFDC this is incorrect advice.

test.stopTest() does not introduce a new set of governor limits, it simply goes back to the context previously used before test.startTest().
So if you made 90 SOQL queries before test.startTest() ran, you only have 10 left to make after test.stopTest().

Also, there is no such thing as an Apex "script statement limit". Assert methods can be run quite safely without worrying about limits.
chris coates 19chris coates 19
IMHO it is good practice to place asserts after the test.stoptest() especially if there is any asynchronous or schedulable (eg method calls with future annotation) code being tested.  The stoptest forces any asynchronous code to 'complete' before returning to the original context.  As such,  by placing the asserts after the stoptest, you know confidently all code encapsulated in the start/stop has finished.