10

Recently one of my colleagues was trying to deploy to production some opportunity trigger activation, but during validation deployment two of ten test methods from TestMyMysteriousApexClass failed with Failure Message: "System.LimitException: Too many SOQL queries: 101".

To investigate this, I have separated these two test methods from other in separate TestDebug class.

However, to my great surprise, these two test methods haven't failed in a separate test class even if that opportunity trigger has active status.

So I just removed those two methods from my TestMyMysteriousApexClass and saved them in TestMyMysteriousApexClassAdditionalTestMethods.

Can anyone explain me how this could be possible at all?

How separation of Test Class TestMyMysteriousApexClass to two distint test classes can solve 101 SOQL Queries Limit Exceeded Error?

Isn't each test method a separate transaction?

Isn't SOQL Queries Limit counted for each test method separately?

Update: It seems that main culprit here is 'public' directive in the beginning of the class:

@isTest
public class TestMyMysteriousApexClass {
// should be:
//private class TestMyMysteriousApexClass {

    private static final String SOME_OBSCURE_STRING_CONSTANT_1 ='Some Obscure String Value 1';
    private static final String SOME_OBSCURE_STRING_CONSTANT_2 ='Some Obscure String Value 2';
    private static final String SOME_OBSCURE_STRING_CONSTANT_3 ='Some Obscure String Value 3';
    private static final String SOME_OBSCURE_STRING_CONSTANT_4 ='Some Obscure String Value 4';
    private static final String SOME_OBSCURE_STRING_CONSTANT_5 ='Some Obscure String Value 5';
    private static final String SOME_OBSCURE_STRING_CONSTANT_6 ='Some Obscure String Value 6';
    private static final String SOME_OBSCURE_STRING_CONSTANT_7 ='Some Obscure String Value 7';
    private static final List<String> someObscureStringList = new List<String>{SOME_OBSCURE_STRING_CONSTANT_1, SOME_OBSCURE_STRING_CONSTANT_2, SOME_OBSCURE_STRING_CONSTANT_3, SOME_OBSCURE_STRING_CONSTANT_4, SOME_OBSCURE_STRING_CONSTANT_5, SOME_OBSCURE_STRING_CONSTANT_6, SOME_OBSCURE_STRING_CONSTANT_7};
    private static final Decimal someObscureDecimal = 5.9;
    private static final Integer SOME_OBSCURE_INTEGER_CONSTANT_1 = 10;
    private static final Integer SOME_OBSCURE_INTEGER_CONSTANT_2 = 10;
    private static final String SOME_OBSCURE_STRING_CONSTANT_8 = 'Some Obscure String Value 8';
    private static final String SOME_OBSCURE_STRING_CONSTANT_9 = 'Some Obscure String Value 9';
    private static final String SOME_OBSCURE_STRING_CONSTANT_10 = 'Some Obscure String Value 10';
    private static final String SOME_OBSCURE_STRING_CONSTANT_11 = 'Some Obscure String Value 11';

    private static SomeObscureCustomObject1__c soco1Instance;
    private static SomeObscureCustomObject2__c soco2Instance1;
    private static SomeObscureCustomObject2__c soco2Instance2;
    private static User testUser;
    private static Opportunity opportunity;
    private static SomeObscureCustomObject3__c soco3Instance;
    private static Account testAccount;
    private static SomeObscureCustomObject4__c soco4Instance;
    private static List<SomeObscureCustomObject2__c> testsoco2Instance1List = new List<SomeObscureCustomObject2__c>();

    private static void setup() {
        setup( false );
    }

    private static void setup( Boolean someObscureBooleanParameter ){
        testUser = TestingUtils.createUser('someStrangeValue', TestingUtils.SYS_ADMIN_PROFILE_ID, true);
        System.runAs(testUser){
            testAccount = TestingUtils.createAccount();
            testAccount.someObscureSyncronizationField1__c = '12345';
            testAccount.someObscureSyncronizationDetector__c = true;
            update testAccount;

            opportunity = TestingUtils.createOpportunities( 1, false )[0];
            opportunity.AccountId = testAccount.Id;
            opportunity.customOppStringField1__c = UserInfo.getLastName() + ',' + Userinfo.getFirstName();
            opportunity.customOppStringField2__c = UserInfo.getLastName() + ',' + Userinfo.getFirstName();
            opportunity.customOppBooleanField__c = someObscureBooleanParameter;
            insert opportunity;


            soco4Instance = new SomeObscureCustomObject4__c( Name = SOME_OBSCURE_STRING_CONSTANT_10 );
            soco4Instance.Opportunity__c = opportunity.Id;
            soco4Instance.CurrencyIsoCode = testingUtils.CURRENCY_ISO_CODE_GBP;

            insert soco4Instance;

            soco3Instance = new SomeObscureCustomObject3__c();
            soco3Instance.Name = SOME_OBSCURE_STRING_CONSTANT_11;
            soco3Instance.RecordTypeId = testingUtils.SOCO3_OBSCURE_RECORD_TYPE_ID;
            soco3Instance.SomeObscureCustomObject4__c = soco4Instance.Id;
            soco3Instance.someObscureDecimal__c = 0;
            soco3Instance.someObscureDecimal_to_Consultant__c = 0;
            soco3Instance.soco3SomeStringField__c = SOME_OBSCURE_STRING_CONSTANT_8;
            soco3Instance.Opportunity__c = opportunity.Id;
            soco3Instance.CurrencyIsoCode = testingUtils.CURRENCY_ISO_CODE_USD;

            insert soco3Instance;

            soco2Instance1 = TestingUtils.createSoco2Instance1( soco4Instance.Id, UserInfo.getUserId(), false );
            soco2Instance1.SomeObscureCustomObject3__c = soco3Instance.Id;
            soco2Instance1.Name = SOME_OBSCURE_STRING_CONSTANT_9;
            testsoco2Instance1List.add( soco2Instance1 );

            soco2Instance2 = TestingUtils.createSoco2Instance1( soco4Instance.Id, UserInfo.getUserId(), false );
            soco2Instance2.SomeObscureCustomObject3__c = soco3Instance.Id;
            soco2Instance2.Name = SOME_OBSCURE_STRING_CONSTANT_9;
            soco2Instance2.Non_Billable__c = true;
            testsoco2Instance1List.add( soco2Instance2 );

            insert testsoco2Instance1List;

            soco1Instance = new SomeObscureCustomObject1__c();
            soco1Instance.someUserIdField__c = Userinfo.getUserId();
            insert soco1Instance;

        }
    }


    private static testmethod void testUpdateTypeForSomeObscureCustomObject6s(){
        testingUtils.avoidGovernorLimit = true;
        setup( true );

        System.runAs(testUser){
            final Date testDate = Date.newInstance(2012, 2, 13);
            List<SomeObscureCustomObject5__c> newSomeObscureCustomObject5s = new List<SomeObscureCustomObject5__c>();

            for( Integer i = 0; i < SOME_OBSCURE_INTEGER_CONSTANT_1; i++ ) {
                Date newDate = testDate.addDays(i*7);
                SomeObscureCustomObject5__c newSomeObscureCustomObject5 = new SomeObscureCustomObject5__c();
                newSomeObscureCustomObject5.Status__c = SomeObscureCustomObject5Services.STATUS_CONSTANT_1;
                newSomeObscureCustomObject5.SomeObscureCustomObject1__c = soco1Instance.Id;
                newSomeObscureCustomObject5.DateField1__c = newDate;
                newSomeObscureCustomObject5.SomeObscureCustomObject2__c = soco2Instance1.Id;
                newSomeObscureCustomObject5.IntegerField1__c = 7;
                newSomeObscureCustomObject5.IntegerField2__c = 7;
                newSomeObscureCustomObject5.IntegerField3__c = 7;
                newSomeObscureCustomObject5.IntegerField4__c = 7;
                newSomeObscureCustomObject5.IntegerField5__c = 7;
                newSomeObscureCustomObject5.CurrencyIsoCode = TestingUtils.CURRENCY_ISO_CODE_USD;

                newSomeObscureCustomObject5s.add( newSomeObscureCustomObject5 );
            }

            insert newSomeObscureCustomObject5s;
            SomeObscureCustomObject6Services.createStagingSomeObscureCustomObject5(newSomeObscureCustomObject5s);
            List<SomeObscureCustomObject6__c> testResultCustomObject6s = [ SELECT Id, AnotherStringField__c, Opportunity__c, Name,Some_Integer_Field_1__c, Some_Integer_Field_2__c
                                                          FROM SomeObscureCustomObject6__c
                                                          WHERE Opportunity__c = : opportunity.Id];

            System.assertEquals (testResultCustomObject6s.size(), SOME_OBSCURE_INTEGER_CONSTANT_1*5);// * 5 days that are filled in

            for (SomeObscureCustomObject6__c SomeObscureCustomObject6Item : testResultCustomObject6s){
                System.assertEquals (SomeObscureCustomObject6Item.AnotherStringField__c, SomeObscureCustomObject6Services.SomeObscureCustomObject5_LOI_TEXT);
                System.assert (SomeObscureCustomObject6Item.Name.contains(SomeObscureCustomObject6Services.SomeObscureCustomObject5_LOI_TEXT));
                System.assertEquals (SomeObscureCustomObject6Item.Some_Integer_Field_1__c, 0);
                System.assertEquals (SomeObscureCustomObject6Item.Some_Integer_Field_2__c, 0);
            }

            Test.startTest();

                soco2Instance2.Non_Billable__c = false;
                update soco2Instance2;

                soco3Instance = [SELECT Id, Name, someObscureDecimal__c, someObscureDecimal_to_Consultant__c
                           FROM SomeObscureCustomObject3__c
                           WHERE Id =:soco3Instance.Id];
                soco3Instance.someObscureDecimal__c = someObscureDecimal;
                soco3Instance.someObscureDecimal_to_Consultant__c = someObscureDecimal*10;
                update soco3Instance;

                opportunity.customOppBooleanField__c = false;

                SomeObscureCustomObject6Services.updateTypeForSomeObscureCustomObject6s(new Map<id, Opportunity>{opportunity.Id => opportunity});

                testResultCustomObject6s = [SELECT Id, AnotherStringField__c, Opportunity__c, Name, Some_Integer_Field_1__c, Some_Integer_Field_2__c
                                     FROM SomeObscureCustomObject6__c
                                     WHERE Opportunity__c = : opportunity.Id];
            Test.stopTest();

            System.assertEquals (testResultCustomObject6s.size(), SOME_OBSCURE_INTEGER_CONSTANT_1*5);// * 5 days that are filled in

            for (SomeObscureCustomObject6__c SomeObscureCustomObject6Item : testResultCustomObject6s){
                System.assertEquals (SomeObscureCustomObject6Item.AnotherStringField__c, SomeObscureCustomObject6Services.SomeObscureCustomObject5_TEXT);
                System.assert (!SomeObscureCustomObject6Item.Name.contains(SomeObscureCustomObject6Services.SomeObscureCustomObject5_LOI_TEXT) && SomeObscureCustomObject6Item.Name.contains(SomeObscureCustomObject6Services.SomeObscureCustomObject5_TEXT));
                System.assertEquals (SomeObscureCustomObject6Item.Some_Integer_Field_1__c, someObscureDecimal);
                System.assertEquals (SomeObscureCustomObject6Item.Some_Integer_Field_2__c, someObscureDecimal*10);
            }
        }
    }

    private static testmethod void testCreateStagingSomeObscureCustomObject5s (){
        testingUtils.avoidGovernorLimit = true;
        setup();

        System.runAs(testUser){

            final Date testDate = Date.newInstance(2012, 2, 13);
            List<SomeObscureCustomObject5__c> newSomeObscureCustomObject5s = new List<SomeObscureCustomObject5__c>();
            List<SomeObscureCustomObject5__c> newSomeOtherObscureCustomObject5s = new List<SomeObscureCustomObject5__c>();
            List<SomeObscureCustomObject5__c> allSomeObscureCustomObject5s = new List<SomeObscureCustomObject5__c>();

            for( Integer i = 0; i < SOME_OBSCURE_INTEGER_CONSTANT_1; i++ ){
                Date newDate = testDate.addDays(i*7);
                SomeObscureCustomObject5__c newSomeObscureCustomObject5 = new SomeObscureCustomObject5__c();
                newSomeObscureCustomObject5.Status__c = SomeObscureCustomObject5Services.STATUS_CONSTANT_1;
                newSomeObscureCustomObject5.SomeObscureCustomObject1__c = soco1Instance.Id;
                newSomeObscureCustomObject5.DateField1__c = newDate;
                newSomeObscureCustomObject5.SomeObscureCustomObject2__c = soco2Instance1.Id;
                newSomeObscureCustomObject5.IntegerField1__c = 7;
                newSomeObscureCustomObject5.IntegerField2__c = 7;
                newSomeObscureCustomObject5.IntegerField3__c = 7;
                newSomeObscureCustomObject5.IntegerField4__c = 7;
                newSomeObscureCustomObject5.IntegerField5__c = 7;
                newSomeObscureCustomObject5.StringField1__c = SOME_OBSCURE_STRING_CONSTANT_1;
                newSomeObscureCustomObject5.StringField2__c = SOME_OBSCURE_STRING_CONSTANT_2;
                newSomeObscureCustomObject5.StringField3__c = SOME_OBSCURE_STRING_CONSTANT_3;
                newSomeObscureCustomObject5.StringField4__c = SOME_OBSCURE_STRING_CONSTANT_4;
                newSomeObscureCustomObject5.StringField5__c = SOME_OBSCURE_STRING_CONSTANT_5;
                newSomeObscureCustomObject5.StringField6__c = SOME_OBSCURE_STRING_CONSTANT_6;
                newSomeObscureCustomObject5.StringField7__c = SOME_OBSCURE_STRING_CONSTANT_7;
                newSomeObscureCustomObject5.CurrencyIsoCode = testingUtils.CURRENCY_ISO_CODE_USD;

                newSomeObscureCustomObject5s.add( newSomeObscureCustomObject5 );
                allSomeObscureCustomObject5s.add( newSomeObscureCustomObject5 );
            }

            for( Integer i = 0; i < SOME_OBSCURE_INTEGER_CONSTANT_2; i++ ){
                Date newDate = testDate.addDays(i*7);
                SomeObscureCustomObject5__c newSomeObscureCustomObject5 = new SomeObscureCustomObject5__c();
                newSomeObscureCustomObject5.SomeObscureCustomObject1__c = soco1Instance.Id;
                newSomeObscureCustomObject5.DateField1__c = newDate;
                newSomeObscureCustomObject5.SomeObscureCustomObject2__c = soco2Instance2.Id;
                newSomeObscureCustomObject5.IntegerField1__c = 7;
                newSomeObscureCustomObject5.IntegerField2__c = 7;
                newSomeObscureCustomObject5.IntegerField3__c = 7;
                newSomeObscureCustomObject5.IntegerField4__c = 7;
                newSomeObscureCustomObject5.IntegerField5__c = 7;
                newSomeObscureCustomObject5.StringField1__c = SOME_OBSCURE_STRING_CONSTANT_1;
                newSomeObscureCustomObject5.StringField2__c = SOME_OBSCURE_STRING_CONSTANT_2;
                newSomeObscureCustomObject5.StringField3__c = SOME_OBSCURE_STRING_CONSTANT_3;
                newSomeObscureCustomObject5.StringField4__c = SOME_OBSCURE_STRING_CONSTANT_4;
                newSomeObscureCustomObject5.StringField5__c = SOME_OBSCURE_STRING_CONSTANT_5;
                newSomeObscureCustomObject5.StringField6__c = SOME_OBSCURE_STRING_CONSTANT_6;
                newSomeObscureCustomObject5.StringField7__c = SOME_OBSCURE_STRING_CONSTANT_7;
                newSomeObscureCustomObject5.CurrencyIsoCode = testingUtils.CURRENCY_ISO_CODE_USD;

                newSomeOtherObscureCustomObject5s.add( newSomeObscureCustomObject5 );
                allSomeObscureCustomObject5s.add( newSomeObscureCustomObject5 );
            }

            insert allSomeObscureCustomObject5s;

            Test.startTest();
                SomeObscureCustomObject6Services.createStagingSomeObscureCustomObject5( allSomeObscureCustomObject5s );
            Test.stopTest();

            List<SomeObscureCustomObject6__c> actualCustomObject6s = [ SELECT Id, String_Field_1__c, Name, Date_Field_1__c, Some_Id_Field__c, String_Field_2__c, OwnerId,
                                                                Consultant_soco2Instance1_Name__c, customOppStringField1__c, customOppStringField2__c, Opportunity_Owner__c,
                                                                Some_Id_Field_2__c, CurrencyIsoCode, AccountId__c
                                                      FROM SomeObscureCustomObject6__c
                                                      WHERE Some_Id_Field__c IN : new Map<Id, SomeObscureCustomObject5__c>( newSomeObscureCustomObject5s ).keySet()
                                                      ORDER BY Date_Field_1__c ASC ];

            List<SomeObscureCustomObject6__c> otherCustomObject6s = [ SELECT Some_Integer_Field_1__c, Some_Integer_Field_2__c, AnotherStringField__c, CurrencyIsoCode, AccountId__c
                                                          FROM SomeObscureCustomObject6__c
                                                          WHERE Some_Id_Field__c IN : new Map<Id, SomeObscureCustomObject5__c>( newSomeOtherObscureCustomObject5s ).keySet()
                                                          ORDER BY Date_Field_1__c ASC ];

            Map<Id, List<SomeObscureCustomObject6__c>> actualCustomObject6sMap = new Map<Id, List<SomeObscureCustomObject6__c>>();

            for( SomeObscureCustomObject6__c CustomObject6Item : otherCustomObject6s ){
                System.assertEquals( opportunity.AccountId, CustomObject6Item.AccountId__c );
                System.assertEquals( 0, CustomObject6Item.Some_Integer_Field_1__c );
                System.assertEquals( 0, CustomObject6Item.Some_Integer_Field_2__c );
                System.assertEquals( SomeObscureCustomObject6Services.SomeObscureCustomObject5_TEXT_OTHER_CONSTANT, CustomObject6Item.AnotherStringField__c );
                System.assertEquals( testingUtils.CURRENCY_ISO_CODE_USD, CustomObject6Item.CurrencyIsoCode );
            }

            for( SomeObscureCustomObject6__c CustomObject6 : actualCustomObject6s ){
                System.assertEquals( opportunity.AccountId, CustomObject6.AccountId__c );
                if( actualCustomObject6sMap.containsKey( CustomObject6.Some_Id_Field__c ) )
                    actualCustomObject6sMap.get( CustomObject6.Some_Id_Field__c ).add( CustomObject6 );
                else
                    actualCustomObject6sMap.put( CustomObject6.Some_Id_Field__c, new List<SomeObscureCustomObject6__c>{ CustomObject6 } );
            }

            System.assertEquals( SOME_OBSCURE_INTEGER_CONSTANT_1 * 5 , actualCustomObject6s.size() );
            System.assertEquals( SOME_OBSCURE_INTEGER_CONSTANT_2 * 5 , otherCustomObject6s.size() );

            Date expectedDate = testDate;

            for( SomeObscureCustomObject5__c newSomeObscureCustomObject5 : newSomeObscureCustomObject5s ){
                List<SomeObscureCustomObject6__c> CustomObject6s = actualCustomObject6sMap.get( newSomeObscureCustomObject5.Id );

                System.assertEquals( 5, CustomObject6s.size() );

                for( SomeObscureCustomObject6__c CustomObject6 : CustomObject6s ){
                     String CustomObject6Name = expectedDate.Month() + '/' + expectedDate.day() + '/' + expectedDate.year() + ' - ' + SomeObscureCustomObject6Services.SomeObscureCustomObject5_TEXT + ' - ' + CustomObject6.String_Field_1__c;

                     System.assertEquals( CustomObject6Name, CustomObject6.Name );
                     System.assertEquals( Userinfo.getUserId(), CustomObject6.Some_Id_Field_2__c );
                     System.assertEquals( expectedDate, CustomObject6.Date_Field_1__c );
                     System.assertEquals( SOME_OBSCURE_STRING_CONSTANT_9, CustomObject6.Consultant_soco2Instance1_Name__c );
                     System.assertEquals( UserInfo.getLastName() + ',' + Userinfo.getFirstName(), CustomObject6.customOppStringField1__c );
                     System.assertEquals( UserInfo.getLastName() + ',' + Userinfo.getFirstName(), CustomObject6.customOppStringField2__c );
                     System.assertEquals( testUser.FirstName  + ' ' + testUser.LastName, CustomObject6.Opportunity_Owner__c );
                     System.assertEquals( Userinfo.getUserId(), CustomObject6.OwnerId );
                     System.assertEquals( testingUtils.CURRENCY_ISO_CODE_USD, CustomObject6.CurrencyIsoCode );

                     expectedDate = expectedDate.addDays( 1 );
                }
                for( Integer i = 1; CustomObject6s.size() > i; i++ ){
                    System.assertEquals( someObscureStringList[i], CustomObject6s[i].String_Field_2__c );
                }
                expectedDate = expectedDate.addDays( 2 );
            }
        }
    }

    private static testmethod void testFilterSomeObscureCustomObject5RecordsOnInsert (){
        setup();
        final Integer NUM_RECORDS = 10;
        final Date testDate = Date.newInstance(2012, 2, 13);
        List<SomeObscureCustomObject5__c> newSomeObscureCustomObject5s = new List<SomeObscureCustomObject5__c>();

        for( Integer i = 0; i < NUM_RECORDS; i++ ){
            Date newDate = testDate.addDays( i * 7 );
            SomeObscureCustomObject5__c newSomeObscureCustomObject5 = new SomeObscureCustomObject5__c();
            newSomeObscureCustomObject5.Status__c = SomeObscureCustomObject5Services.STATUS_CONSTANT_1;
            newSomeObscureCustomObject5.SomeObscureCustomObject1__c = soco1Instance.Id;
            newSomeObscureCustomObject5.DateField1__c = newDate;
            newSomeObscureCustomObject5.SomeObscureCustomObject2__c = soco2Instance1.Id;
            newSomeObscureCustomObject5.IntegerField1__c = 7;
            newSomeObscureCustomObject5.IntegerField2__c = 7;
            newSomeObscureCustomObject5.IntegerField3__c = 7;
            newSomeObscureCustomObject5.IntegerField4__c = 7;
            newSomeObscureCustomObject5.IntegerField5__c = 7;
            newSomeObscureCustomObject5.Boolean_Some_Status_Field__c = false;
            newSomeObscureCustomObject5.CurrencyIsoCode = testingUtils.CURRENCY_ISO_CODE_GBP;
            newSomeObscureCustomObject5s.add(newSomeObscureCustomObject5);
        }
        insert newSomeObscureCustomObject5s;

        for(Integer i=0; i< NUM_RECORDS/2 ; i++)
            newSomeObscureCustomObject5s[i].Boolean_Some_Status_Field__c = true;

        List<SomeObscureCustomObject5__c> actualSomeObscureCustomObject5s;
        Test.startTest();
            actualSomeObscureCustomObject5s = SomeObscureCustomObject6Services.filterSomeObscureCustomObject5Records( null, new Map<Id, SomeObscureCustomObject5__c> ( newSomeObscureCustomObject5s ) );
        Test.stopTest();

        System.assertEquals( NUM_RECORDS/2 , actualSomeObscureCustomObject5s.size() );
    }

    private static testmethod void testFilterSomeObscureCustomObject5RecordsOnUpdate (){
        setup();
        final Integer NUM_RECORDS = 10;
        final Date testDate = Date.newInstance(2012, 2, 13);
        List<SomeObscureCustomObject5__c> newSomeObscureCustomObject5s = new List<SomeObscureCustomObject5__c>();

        for( Integer i = 0; i < NUM_RECORDS; i++ ){
            Date newDate = testDate.addDays( i * 7 );
            SomeObscureCustomObject5__c newSomeObscureCustomObject5 = new SomeObscureCustomObject5__c();
            newSomeObscureCustomObject5.Status__c = SomeObscureCustomObject5Services.STATUS_CONSTANT_1;
            newSomeObscureCustomObject5.SomeObscureCustomObject1__c = soco1Instance.Id;
            newSomeObscureCustomObject5.DateField1__c = newDate;
            newSomeObscureCustomObject5.SomeObscureCustomObject2__c = soco2Instance1.Id;
            newSomeObscureCustomObject5.IntegerField1__c = 7;
            newSomeObscureCustomObject5.IntegerField2__c = 7;
            newSomeObscureCustomObject5.IntegerField3__c = 7;
            newSomeObscureCustomObject5.IntegerField4__c = 7;
            newSomeObscureCustomObject5.IntegerField5__c = 7;
            newSomeObscureCustomObject5.Boolean_Some_Status_Field__c = false;
            newSomeObscureCustomObject5.CurrencyIsoCode = testingUtils.CURRENCY_ISO_CODE_USD;

            newSomeObscureCustomObject5s.add(newSomeObscureCustomObject5);
        }
        insert newSomeObscureCustomObject5s;

        List<SomeObscureCustomObject5__c> testSomeObscureCustomObject5s = newSomeObscureCustomObject5s.deepClone(true);

        for(Integer i=0; i< NUM_RECORDS/2 ; i++)
            testSomeObscureCustomObject5s[i].Boolean_Some_Status_Field__c = true;

        List<SomeObscureCustomObject5__c> actualSomeObscureCustomObject5s;
        Test.startTest();
            actualSomeObscureCustomObject5s = SomeObscureCustomObject6Services.filterSomeObscureCustomObject5Records( new Map<Id, SomeObscureCustomObject5__c> ( newSomeObscureCustomObject5s ), new Map<Id, SomeObscureCustomObject5__c> ( testSomeObscureCustomObject5s ) );
        Test.stopTest();

        System.assertEquals( NUM_RECORDS/2 , actualSomeObscureCustomObject5s.size() );
    }
// some other obscure test methods after that....

so, when I change visibility of test class from public to private, 101 SOQL Queries LIMIT Exception disappears also...

So, the methods which are failing if they are together with other methods with 'public' test class are testFilterSomeObscureCustomObject5RecordsOnInsert and testFilterSomeObscureCustomObject5RecordsOnUpdate/

New Update.

Today I tried again and it seems that file was failing even though had 'private' declaration in the beginning. I have looked then at the version number thoroughly again, and it seems that API version matters, so actually TestMyMysteriousApexClass has API Version number not 26 but 23, while TestDebug has API Version number 28.

It seems George S. was most close to the solution in his comment

Does TestMyMysteriousApexClass have the same API Version as TestDebug? –  George S. 

However, I still don't really understand how API Version number of a class can impact on SOQL Queries limit calculation...

10
  • The easy answers to your last two questions are yes and yes. The others are extremely intriguing. But I'm afraid we will struggle to answer this without seeing the code. But the obvious question is "what else is different besides them being in a different class?" There must be some other factor that has changed, or some upstream/downstream factor that behaves differently now that this code is in its own class.
    – pchittum
    Jan 16, 2014 at 11:58
  • 1
    Does TestMyMysteriousApexClass have the same API Version as TestDebug?
    – George S.
    Jan 16, 2014 at 12:19
  • George S., TestMyMysteriousApexClass has API version 28 and TestDebug has API version 29, TestMyMysteriousApexClassAdditionalTestMethods has API version 28, so I don't think this could matter.
    – Patlatus
    Jan 16, 2014 at 12:23
  • Peter, both classes have the same code. The only thing which is different is declaration of the TestMyMysteriousApexClass, it is declared as "public" instead of "private" @isTest public class TestMyMysteriousApexClass
    – Patlatus
    Jan 16, 2014 at 12:28
  • You wondered about the source code, I have inserted excerpt from it to my question declaration.
    – Patlatus
    Jan 16, 2014 at 15:03

3 Answers 3

2

EDIT:

It seems George S. was most close to the solution in his comment

Does TestMyMysteriousApexClass have the same API Version as TestDebug? – George S.

However, I still don't really understand how API Version number of a class can impact on SOQL Queries limit calculation...

Because classes saved with API v 23 and lower act as if they have IsTest(SeeAllData=true) ;)


In no specific order...

  • Is @SeeAllData present in the test? Is the trigger calling classes that have different with/without sharing (this thing has tri-state logic: with, without and blank; blank means "inherit from parent that called my methods))? I think SeeAllData is sometimes needed when testing Opportunities because the Pricebook object is nasty.
  • Does the code (triggers/workflows) have anything User-specific? You probably have same Profiles, but what about roles, groups, maybe even amount of owned opportunities (just guessing what the trigger might be doing)? Some hierarchy custom setting maybe? For consistent (if cumbersome) results you should be testing with System.runAs(user-generated-in-test-class) where possible.
  • Now that the trigger is activated - what happens your colleague will try to run all tests in production?
  • Anything random happening in the tests? ;)
3
  • No, @SeeAllData is not present in my test.
    – Patlatus
    Jan 16, 2014 at 14:31
  • We have complicated logic so I can't really answer if test methods being called are firing triggers which are calling classes that have different with/without sharing
    – Patlatus
    Jan 16, 2014 at 14:33
  • We use sometimes System.runAs, however this doesn't answer my question and doesn't seem to help. No randomization in the tests.
    – Patlatus
    Jan 16, 2014 at 14:34
1

One safe thing i would consider in these type of situation is to have Test.StartTest() and Test.StopTest() as those help to refresh the context and hence avoid governor limit condition.Also I am not sure if you have enabled parallel execution or disabled.Practically this should not matter but doing Test.StartTest() and Test.StopTest() is essential to refresh context limits

2
  • 4
    I have in each test methods Test.StartTest() and Test.StopTest() but this neither answers my question nor helps. Your post doesn't really seem to be related to what I am asking about.
    – Patlatus
    Jan 16, 2014 at 14:31
  • Actually, this answer is exactly what's posted here too: stackoverflow.com/questions/12009698/…
    – eyedar
    Nov 11, 2014 at 1:48
1

Here's a shell script I wrote to help count soql statements from debug logs. $1 is the filename of the debug log in question. Hope this helps.

#!/bin/bash

grep SOQL_EXECUTE_BEGIN $1 | awk 'BEGIN { FS = "|" }; { print $5}'| sort | uniq -c -d 
3
  • Does that refer to linux OS? First of all, I have Windows OS.
    – Patlatus
    Jan 17, 2014 at 8:32
  • The second, I don't understand and have no idea how anyone can use it for salesforce since no local version of cloud is available and no chance to debug it on own machine
    – Patlatus
    Jan 17, 2014 at 8:33
  • yes, you can run this as a bash script on linux/osx. on windows something like cygwin could run it as well. You would run this file against the downloaded debug log of your test. the script itself finds all soql run during your tests, and counts how many times each individual query was run. It's a good way to find soql in loops and whatnot.
    – donjay
    Jan 17, 2014 at 20:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .