0

I have Added Limit to the Query and in Test Class @isTest(SeeAllData=false), if(!Test.isRunningTest()) -Every Solution I found Here .Even it's not working here .So please any one can Help?

Apex Class

global class ChapterRollupBatch implements Database.Batchable<sObject>
{
   global Database.QueryLocator start(Database.BatchableContext BC)
    {
        //return Database.getQueryLocator([select Id, Name, Type, 
         ChatterGroupId__c from Account where ChatterGroupId__c = null]);
          return Database.getQueryLocator([select Id, Name, 
                         Type,NumberOfPledges__c, NumberOfUndergraduates__c, 
                                                      NumberOfAlumnus__c from 
             Account where type = 'Chapter' OR type = 'Colony' LIMIT 1]);
    }
    global void execute(Database.BatchableContext BC, List<Account> ChapterList)
    {

        for (Account Chapter : ChapterList)
        {
            Chapter.ACA_Number_of_Pledges__c = [SELECT Count() FROM 
                              Association__c where Pledge_POO__c = 1 AND 
                             Chapter__c =: Chapter.Id ];
            Chapter.ACA_Number_of_Initiates__c = [SELECT Count() FROM 
                              Association__c Where Initiate_POO__c = 1 AND 
                             Chapter__c =: Chapter.Id ];
            Chapter.ACA_Number_of_Alumni_Sans_Celestial__c = [SELECT Count() FROM 
                              Association__c WHERE Alumni_POO__c = 1 AND 
                             Chapter__c =: Chapter.Id ];
            Chapter.of_Cases__c = [SELECT Count() FROM Case WHERE AccountId = 
                             :Chapter.Id];
            Chapter.Certs_held_by_UGs__c = [SELECT Count() FROM AdditionalInfo__c 
                              WHERE Student__r.PreferredChapter__c = :Chapter.Id 
                                       AND Student__r.ACA_Member_Role_B__c = 
                                                     'Initiate' AND 
                                    (AwardStatus__c = 'Approved' OR 
                              AwardStatus__c = 'Recipient')];
            Chapter.Of_Initiates_Registered__c = [SELECT Count() FROM User WHERE 
                                     Contact.ACA_Member_Role_B__c = 'Initiate' 
                                  AND Contact.PreferredChapter__c =: Chapter.Id];
            Chapter.Of_Pledges_Registered__c = [SELECT Count() FROM User WHERE 
                                    Contact.ACA_Member_Role_B__c = 'Pledge' AND 
                                       Contact.PreferredChapter__c =: 
                                  Chapter.Id];
            Chapter.Number_of_Initiates_All_Time__c =  [SELECT Count() FROM 
                                     Contact WHERE AccountId =:Chapter.Id AND 
                                InitiationDate__c != null ];
            update Chapter;
        }
           }
    global void finish(Database.BatchableContext bc)
    {
        if(!Test.isRunningTest())
        {
            //Execute the Chapter Dashboard Batch
            database.executeBatch(new ChapterDashboardBatch(), 1);

        }
    }
}

Test Class


  @isTest(SeeAllData=false)
public class TestChapterRollupBatch {
    static testMethod void testMethod1() 
    {   
        try{
            List<Contact> lstcontact= new List<Contact>();
            for(Integer i=0 ;i <5;i++)
            {
                Contact con = new Contact();
                con.LastName ='ContactName'+i;
                lstcontact.add(con);
            }

            insert lstcontact;

            List<Account> lstAccount= new List<Account>();
            for(Integer i=0 ;i <1;i++)
            {
                Account acc = new Account();
                acc.Name ='Name'+i;
                acc.type ='Chapter';
                acc.ContactName__c = lstcontact[i].LastName;
                lstAccount.add(acc);
            }

            insert lstAccount;

            Test.startTest();
            ChapterRollupBatch obj = new ChapterRollupBatch();
            obj.execute(null, lstAccount);
            Id batchID = Database.executeBatch(obj,200);
            System.abortJob(batchID);
            Test.stopTest();
        }
        Catch(Exception e)
        {
            system.debug('Exception:'+e+e.getLineNumber());
        }

    }
}
2
  • What line specifically is the error occurring on? Nov 19, 2019 at 12:59
  • BTW, your execute() method will blow up on too many SOQL if the batch size is more than a handful of records because you are doing SOQL in a for loop; your loop also will blow up on DML calls on the update
    – cropredy
    Nov 20, 2019 at 2:24

2 Answers 2

2

This error actually means that the execute method should be called only ones. In order to achieve this, you have to be sure that:

1) Your query in the start method returns all records in the test context that are available for the batch. In your example, it should be 1. So, check that only one account is present as you have LIMIT 1.

2) Remove (SeeAllData=false) as it is false by defaults

3) You DO NOT NEED to do an explicit invocation of the execute method (obj.execute(null, lstAccount);) and abort the batch (System.abortJob(batchID);), just write:

Test.startTest();
ChapterRollupBatch obj = new ChapterRollupBatch();
Database.executeBatch(obj,200);
Test.stopTest();

The batch will be executed after Test.stopTest(); and only one invocation of execute will happen

2
  • Hello,I appreciate your effort Sir.But unfortunately i tried all these 3 steps but still the same error is there .I am stuck now. System.UnexpectedException: No more than one executeBatch can be called from within a test method. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.
    – user70345
    Nov 20, 2019 at 4:15
  • @AthiraRamadas could you debug how many items are returned by System.debug([SELECT Name FROM Account WHERE Type = 'Chapter' OR type = 'Colony']) query if you place it in the batch start method? Nov 20, 2019 at 9:19
0

The problem was in Contact as i was causing error for some other reasons when i removed contact part from test class i got the answer Test Class

@isTest public class TestChapterRollupBatch { static testMethod void testMethod1() {
List lstAccount= new List(); for(Integer i=0 ;i<10;i++) { Account acc = new Account(); acc.Name ='Name'+i; acc.type ='Chapter'; lstAccount.add(acc); }

    insert lstAccount;
    System.debug('lstAccount');
    Test.startTest();
    ChapterRollupBatch obj = new ChapterRollupBatch();
    Database.executeBatch(obj,200);
    Test.stopTest();


}

}

You must log in to answer this question.