+ Start a Discussion
Luke Osmundson 2Luke Osmundson 2 

Test now failing in production

A long working Test Class of mine is now failing in production due to too many SOQL calls on the second to last call. I tried adding in test.start() and test.stop() functions, which should have reduced the number of calls by 22, but it still fails at what appears to be the exact same place when I try and push the test class. I can't even work on anything else until this resolves, as it causes everything I try to push to fail. Does anyone have any clue what might be happening?
UC InnovationUC Innovation
Hi Luke,

This is currently something that i too am faced with. Seems like test.StartTest and test.StopTest() do increase your governor limits within the test start/stop block but dont reset it after the stop call. This is undocumented (or is unclear to me from the documentation upon reading) but it seems like this is the issue. I got arround this by placing my query/dml inside of the start/stop block and it seems to work just fine for me.

Hope this helps!

AM
Derek M HughesDerek M Hughes
In my (fairly recent) experience with a similar issue the root cause was a trigger that used to called once and was now being called twice whcih suddenly pushed up the SOQL statements.  And it was being called a second time due to seemingly unrelated changes to workflows / processes which updated fields as well as a change to one of the triggers.  This took us a while to figure out.  I would suggest you look at see what triggers are firing during your test and see if the problem originates there.

The code before test.StartTest has all the same limits as a normal code execution.  test.StartTest resets these limits back to 0 and gives you a full set of limits for your actaul test code.  As far as I know the statements after test.StopTest are counted with your test code.
 
Jamal RidaJamal Rida
Hi Luke,

It is true that Test.startTest reset the limits to 0, but may be inside your test method you are doing multiples calls to the same method as example : 
Test.startTest();
Controller ctrl = new Controller(myCase);
ctrl.save();
myCase.field__c = 'xxx';
update myCase.
ctrl.save();
myCase.field__c = 'yyyy';
update myCase;
ctrl.save();
....
Test.stopTest();

You will hit limits like this if it's the case since there are Triggers which means cumulative  SOQL Queries. i suggest to split into several test Methods where you can test Case by case.