function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ritesh__Ritesh__ 

System.UnexpectedException: No more than one executeBatch can be called from within a testmethod

i a testing my app . it is giving me error

System.UnexpectedException: No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.

i am trying to call a Batch apex class when this app is based on Google Calendar Api.thats why json response .but i am facing the above error unable to resolve.

@isTest public class TestBatchCleanGCalendars {

    static testMethod void testBatchJob() {

        GoogleTestHelper.createfullGSettings(10);
        GoogleTestHelper.createCampaigns();

        List<Campaign> campaigns = [Select Id, Name from Campaign];

        system.assertEquals(5,campaigns.size());

        GoogleTestHelper.createTestCalendars();

        GCalendarUtil.testResponse = GoogleTestHelper.returnGCalendarResp();
            BatchCleanGCalendars bJob = new BatchCleanGCalendars('Select Id, Name, CalendarId__c,CalendarQuery__c,FieldToDisplay__c from CalendarSettings__c');

        bJob.testResponse = '{'
+' "kind": "calendar#events",'
+' "summary": "Saarthak",'
+' "description": "",'
+' "updated": "2013-03-26T10:17:05.354Z",'
+' "timeZone": "Asia/Calcutta",'
+' "accessRole": "owner",'
+' "items": ['
+'  {'  +'  "kind": "calendar#event",'   +' "id": "c3gmu1me12tmujuifgmbrr33rc",'  +'  "status": "confirmed",'
+  ' "htmlLink": "https://www.google.com/calendar/event?eid=YzNnbXUxbWUxMnRtdWp1aWZnbWJycjMzcmMgamg5NWRyMDhpNDdwZ3RkMzFmMzQ5Z25uZmdAZw",'
+'  "created": "2013-03-26T10:17:03.000Z",'   +' "updated": "2013-03-26T10:17:03.552Z",'   +' "summary": "GC Product Webinar - Jan 7, 2002",'   +' "description": "null",'   +' "creator": {'   +'  "email": "riteshmehandiratta@gmail.com"'   +' },'   +' "organizer": {'
+'  "email": "jh95dr08i47pgtd31f349gnnfg@group.calendar.google.com",'   +'  "displayName": "Saarthak",'   +'  "self": true'   +' },'   +' "start": {'    +' "date": "2006-09-04"'   +' },'  +'  "end": {'   +'  "date": "2006-09-04"'   +' },'   +' "iCalUID": "c3gmu1me12tmujuifgmbrr33rc@google.com",'   +' "sequence": 0,'   +' "reminders": {'    +' "useDefault": true'  +'  }'  +' },'  +' {'   +' "kind": "calendar#event",'   +' "id": "6se2vm8guhbh2tekc7scnpcudc",'  
+' "status": "confirmed",'   +' "htmlLink": "https://www.google.com/calendar/event?eid=NnNlMnZtOGd1aGJoMnRla2M3c2NucGN1ZGMgamg5NWRyMDhpNDdwZ3RkMzFmMzQ5Z25uZmdAZw",'
+' "created": "2013-03-26T10:17:04.000Z",'   +' "updated": "2013-03-26T10:17:04.096Z",'   +' "summary": "User Conference - Jun 17-19, 2002",'   +' "description": "null",'   +' "creator": {'   +'  "email": "riteshmehandiratta@gmail.com"'   +' },'   +' "organizer": {'
+'  "email": "jh95dr08i47pgtd31f349gnnfg@group.calendar.google.com",'   +'  "displayName": "Saarthak",'   +'  "self": true'   +' },'   +' "start": {'   +'  "date": "2007-02-12"'   +' },'   +' "end": {'    +' "date": "2007-02-14"'   +' },'   +' "iCalUID": "6se2vm8guhbh2tekc7scnpcudc@google.com",'   +' "sequence": 0,'   +' "reminders": {'    +' "useDefault": true'   +' }'  +' },'  +' {'  +'  "kind": "calendar#event",'  +'  "id": "8of1l1ffqbfmm582und3q42k10",' 
+'  "status": "confirmed",'   +' "htmlLink": "https://www.google.com/calendar/event?eid=OG9mMWwxZmZxYmZtbTU4MnVuZDNxNDJrMTAgamg5NWRyMDhpNDdwZ3RkMzFmMzQ5Z25uZmdAZw",'
+' "created": "2013-03-26T10:17:04.000Z",'   +' "updated": "2013-03-26T10:17:04.707Z",'   +' "summary": "DM Campaign to Top Customers - Nov 12-23, 2001",'   +' "description": "null",'   +' "creator": {'   +'  "email": "riteshmehandiratta@gmail.com"'   +' },' 
+'"organizer": {'   +'  "email": "jh95dr08i47pgtd31f349gnnfg@group.calendar.google.com",'   +'  "displayName": "Saarthak",'   +'  "self": true'   +' },'   +' "start": {'   +'  "date": "2006-07-10"'   +' },'   +' "end": {'    +' "date": "2006-07-21"'  +'  },'   +' "iCalUID": "8of1l1ffqbfmm582und3q42k10@google.com",'   +' "sequence": 0,'   +' "reminders": {'   +'  "useDefault": true'   +' }'  +' }'
+' ]'
+',"nextPageToken": "CigKGjFhaW9oc25nZTFhZzBxYTY4bjZhOGlrdG5nGAEggICA0arZqecT"'
+'}';

 bJob.testResponse1 = '{'
+' "kind": "calendar#events",'
+' "summary": "Saarthak",'
+' "description": "",'
+' "updated": "2013-03-26T10:17:05.354Z",'
+' "timeZone": "Asia/Calcutta",'
+' "accessRole": "owner",'
+' "items": ['
+'  {'  +'  "kind": "calendar#event",'   +' "id": "c3gmu1me12tmujuifgmbrr33rc",'  +'  "status": "confirmed",'
+  ' "htmlLink": "https://www.google.com/calendar/event?eid=YzNnbXUxbWUxMnRtdWp1aWZnbWJycjMzcmMgamg5NWRyMDhpNDdwZ3RkMzFmMzQ5Z25uZmdAZw",'
+'  "created": "2013-03-26T10:17:03.000Z",'   +' "updated": "2013-03-26T10:17:03.552Z",'   +' "summary": "GC Product Webinar - Jan 7, 2002",'   +' "description": "null",'   +' "creator": {'   +'  "email": "riteshmehandiratta@gmail.com"'   +' },'   +' "organizer": {'
+'  "email": "jh95dr08i47pgtd31f349gnnfg@group.calendar.google.com",'   +'  "displayName": "Saarthak",'   +'  "self": true'   +' },'   +' "start": {'    +' "date": "2006-09-04"'   +' },'  +'  "end": {'   +'  "date": "2006-09-04"'   +' },'   +' "iCalUID": "c3gmu1me12tmujuifgmbrr33rc@google.com",'   +' "sequence": 0,'   +' "reminders": {'    +' "useDefault": true'  +'  }'  +' },'  +' {'   +' "kind": "calendar#event",'   +' "id": "6se2vm8guhbh2tekc7scnpcudc",'  
+' "status": "confirmed",'   +' "htmlLink": "https://www.google.com/calendar/event?eid=NnNlMnZtOGd1aGJoMnRla2M3c2NucGN1ZGMgamg5NWRyMDhpNDdwZ3RkMzFmMzQ5Z25uZmdAZw",'
+' "created": "2013-03-26T10:17:04.000Z",'   +' "updated": "2013-03-26T10:17:04.096Z",'   +' "summary": "User Conference - Jun 17-19, 2002",'   +' "description": "null",'   +' "creator": {'   +'  "email": "riteshmehandiratta@gmail.com"'   +' },'   +' "organizer": {'
+'  "email": "jh95dr08i47pgtd31f349gnnfg@group.calendar.google.com",'   +'  "displayName": "Saarthak",'   +'  "self": true'   +' },'   +' "start": {'   +'  "date": "2007-02-12"'   +' },'   +' "end": {'    +' "date": "2007-02-14"'   +' },'   +' "iCalUID": "6se2vm8guhbh2tekc7scnpcudc@google.com",'   +' "sequence": 0,'   +' "reminders": {'    +' "useDefault": true'   +' }'  +' },'  +' {'  +'  "kind": "calendar#event",'  +'  "id": "8of1l1ffqbfmm582und3q42k10",' 
+'  "status": "confirmed",'   +' "htmlLink": "https://www.google.com/calendar/event?eid=OG9mMWwxZmZxYmZtbTU4MnVuZDNxNDJrMTAgamg5NWRyMDhpNDdwZ3RkMzFmMzQ5Z25uZmdAZw",'
+' "created": "2013-03-26T10:17:04.000Z",'   +' "updated": "2013-03-26T10:17:04.707Z",'   +' "summary": "DM Campaign to Top Customers - Nov 12-23, 2001",'   +' "description": "null",'   +' "creator": {'   +'  "email": "riteshmehandiratta@gmail.com"'   +' },' 
+'"organizer": {'   +'  "email": "jh95dr08i47pgtd31f349gnnfg@group.calendar.google.com",'   +'  "displayName": "Saarthak",'   +'  "self": true'   +' },'   +' "start": {'   +'  "date": "2006-07-10"'   +' },'   +' "end": {'    +' "date": "2006-07-21"'  +'  },'   +' "iCalUID": "8of1l1ffqbfmm582und3q42k10@google.com",'   +' "sequence": 0,'   +' "reminders": {'   +'  "useDefault": true'   +' }'  +' }'
+' ]'
+'}';
        Test.StartTest();

        System.assertEquals(2,[Select count() from CalendarSettings__c ]);

               DataBase.executeBatch(bJob); 


        Test.StopTest();


    }

after completing Batch request in finish method i am calling one more Batch class.can any one please point me why i am getting this error when i am calling one Batch class in testmethod ?? please some one help

 

Shailesh DeshpandeShailesh Deshpande
While you can call batch class from another batch class, you afre allowed to call only one batch class from Test classes. Hence you are facing this error.

The alternative for this is to check in the finish method of the calling Batch class whether you are in a test context or regular context and call tha 2nd Batch Class only while you are in regular context. You can do this using Test.isRunningTest() in your 1st Batch Class.