[SalesForce] Too many SOQL queries: 101 issue with Unit Test Methods

We are facing couple of issue with Unit Test methods.

  1. SOQL queries limit hits before "Test.StartTest()". We want to test a batch process and it needs to creates records for around 8 to 10 objects which has triggers. Because of that it is throwing limit exception before "Test.StartTest()".

Below is example of code.

@isTest
private class Test_Trigger_Contact
 {
     static testMethod void test1() 
     {        
       Student__c  studentToAdd; 
       List<Contact> contacts= new List<Contact>();  

      // To create Test data we are creating some test records of around 8-10 objects.
      // Internally it fires trigger and consumes diffrent SOQL queries, which cross 101 SOQL limit.
      // Because of this our test class fails before "Test.StartTest()"/"Test.StopTest()".
       for(integer i=0;i<101;i++)
         {
               contacts= [SELECT Id,Name FROM   Contact];

         }        
       test.startTest();
      // Do Some Unit Test for a batch process
      test.stopTest();
     }
 }
  1. Another problem is, Salesforce is not resetting the Governor limit inside "Test.StartTest()" and "Test.StopTest()". Which is mentioned on below Salesforce issue.

https://success.salesforce.com/issues_view?id=a1p30000000T5R5AAK

With below example, you can reproduce this issue :

 @isTest
 private class Test_Trigger_Contact
 {
     static testMethod void test1() 
     {        
       Student__c  studentToAdd; 
       List<Contact> contacts= new List<Contact>();  

      // Below code is consuming 98 queries to generate Test Data
       for(integer i=0;i<98;i++)
         {
               contacts= [SELECT Id,Name FROM   Contact];

         }   
      // After that we are using "test.startTest()", to reset governer imit.
      // But it doesn't and throws 101 SOQL query error.
       test.startTest();
      // Do Some Unit Test for a batch process
       for(integer i=0;i<3;i++)
       {
               contacts= [SELECT Id,Name FROM   Contact];

         }  
      test.stopTest();
     }
 }

2nd issue will be fixed with Salesforce release as mentioned on above link.

I am not sure how can we fix 1st issue. Where we need to generate large data to test Batch Process. My understanding was, Salesforce calculates governor limits only inside "Test.startTest()" and "Test.stopTest()" for unit Test method and it doesn't calculates it while generating test data. Can someone please confirm my understanding regarding?

Thanks and Regards,
Ashish Shukla

Best Answer

By using the Test.startTest() & Test.stopTest() you get 2X governer limits.

In nutshell you have 2 context.

  1. out side of Test.startTest() & Test.stopTest() and
  2. inside of Test.startTest() & Test.stopTest().

In both context you get separate limits.

To fix this issue what you will have to do is:

  1. Optimize and see if you can reduce the queries in trigger
  2. Reduce the number of objects you are inserting
  3. You can skip unnecessary trigger execution by checking if trigger being called by Test method if(Test.isRunningTest())
  4. in worst case you can use SeeAllData = ture