In the below unit test my debug line

System.debug('+++ Number of Queries used in this apex code 2.1 so far: ' + Limits.getQueries());

shows that the number of queries after my single query, in a new method, is 100. My understanding was that each method is has it's own set of limits (although I can't find the documentation to confirm this), in which case shouldn't the number of queries should be 1?

This is causing me to hit the error

System.LimitException: Too many SOQL queries: 101

so I'm unable to retrieve the updated records & assert the results of my test.

@isTest
public class WE_MRFv3Test {

    @testSetup
    static void dataSetup() {

        User u = WE_TestDataUtility.createStandardUser();

        System.runas(u) {

            VApexC__c acCS = WE_TestDataUtility.createApexControllerCustSett(
                'Opportunity MRF',  // fieldRef
                false);             // fcstDisabledStatus

            acCS.BudgetManagerDisabled__c = true;

            insert acCS;

            VProfileId__c profCS = WE_TestDataUtility.createProfileIdCustSett(
                'User',                 // locationRef
                'System Admin User',    // fieldRef
                true);                  // settingStatus

            profCS.Team_Name__c = 'Virtual Admin';

            insert profCS;

            VRTN__c rtCS = WE_TestDataUtility.createRecTypeNameCustSett(
                'CP Virtual Prepaid MC',    // rtName
                'NAEU Opps');               // csField

            rtCS.Product_Name__c = 'Virtual Prepaid MasterCard';

            insert rtCS;

            List<Target__c> targets = WE_TestDataUtility.createTargets(
                2,                                  // noMonths
                u.Id,                               // salespersonId
                date.today().addMonths(1).year(),   // firstYear
                date.today().addMonths(1).month(),  // firstMonth
                'Virtual Sales EU',                 // team
                'Close Date',                       // dateType
                100000);                            // target

            insert targets;

            List<Budget__c> budgets = new List<Budget__c>();

            for(integer i = 0; i < 24; i++) {
                budgets.add(new Budget__c(
                    Name = 'Test Budget ' + i,
                    Date__c = date.today().addMonths(i),
                    Team__c = 'Virtual Admin',
                    Product__c = 'Virtual Prepaid MasterCard',
                    Budget__c = 1));
            }
            insert budgets;

            List<Opportunity> oppsToInsert = new List<Opportunity>();

            List<Opportunity> opportunities0 = WE_TestDataUtility.createOpportunities(
                'EU Opps',                          // oppRef
                1,                                  // noOpps
                'CP Virtual Prepaid MC',            // recordTypeName
                'id0',                              // identifier
                '1) Suspect',                       // stageName
                date.today().addmonths(1),          // closeDate
                'Standard',                         // rampProfile
                100.00);                            // settlement

            insert opportunities0;

            acCS.BudgetManagerDisabled__c = false;
            update acCS;

            List<Opportunity> opportunities1 = WE_TestDataUtility.createOpportunities(
                'EU Opps',                          // oppRef
                1,                                  // noOpps
                'CP Virtual Prepaid MC',            // recordTypeName
                'id1',                              // identifier
                '1) Suspect',                       // stageName
                date.today().addmonths(1),          // closeDate
                'Standard',                         // rampProfile
                100.00);                            // settlement

            oppsToInsert.addAll(opportunities1);

            List<Opportunity> cappedFcstDurOpps = WE_TestDataUtility.createOpportunities(
                'EU Opps',                          // oppRef
                1,                              // noOpps
                'CP Virtual Prepaid MC',        // recordTypeName
                'id2',                          // identifier
                '1) Suspect',                   // stageName
                date.today().addMonths(1),      // closeDate
                'Standard',                     // rampProfile
                100.00);                        // settlement

            for(Opportunity o : cappedFcstDurOpps) {
                o.FSR_End_Date__c = date.today().addMonths(9);
                oppsToInsert.add(o);
            }

            insert oppsToInsert;
        }
    }

    static testMethod void testMrfUpdates2() {

        List<Opportunity> opportunities1 = [SELECT Id, CloseDate FROM Opportunity WHERE Name LIKE '%Id1%'];

        System.debug('+++ Number of Queries used in this apex code 2.1 so far: ' + Limits.getQueries());

        test.startTest();

        for(Opportunity upOppsFcstAdj : opportunities1) {
            upOppsFcstAdj.Forecast_Adjustment__c = 10;
        }
        update opportunities1;

        for(Opportunity upOppsRampProfile : opportunities1) {
            upOppsRampProfile.FSR_End_Date__c = upOppsRampProfile.CloseDate.addMonths(6);
        }
        update opportunities1;

        test.stopTest();

        System.debug('+++ Number of Queries used in this apex code 2.2 so far: ' + Limits.getQueries());

        // too many queries error on next line
        List<Monthly_Revenue_Forecast__c > mrfRecs1 = [SELECT Id, Budget__c
                                                       FROM Monthly_Revenue_Forecast__c 
                                                       WHERE Opportunity__c IN :opportunities1];

        // 1 is noOpps
        system.assertEquals(1*3, mrfRecs1.size(), 'there should be 3 MRF records for each Opportunity, as the forecast duration is capped');
        system.assert(mrfRecs1[0].Budget__c != null, 'budget should have been matched with MRF record');
    }

}
up vote 2 down vote accepted

If your org has events that are triggered from dml statements during @testSetup (which you likely do), THEN any DML or SOQL statement that results from those events are counted outside the context of @testSetup, in the test's other methods. So this will impact the overall governing limits for your test class.

Check this thread for more information related to limits & @TestSetup

  • I'm hitting the limit outside startTest stopTest not within that context. – Alex S Mar 17 '16 at 5:08
  • In that case when you insert the record your trigger is running which is causing this error. Check your triggers – Tushar Sharma Mar 17 '16 at 5:09
  • The question I'm asking is why am I at 100 queries having just entered a new method and executed one query? – Alex S Mar 17 '16 at 5:11
  • As I mentioned your trigger also running and if they are doing any SOQL that will also count in this limit. – Tushar Sharma Mar 17 '16 at 5:13
  • If you look at the location of my debug line, you'll see that I've not executed any transactions in my method before I check the limits. – Alex S Mar 17 '16 at 5:15

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.