System.UnexpectedException:在testmethod中只能调用一个executeBatch

时间:2013-03-27 05:28:32

标签: salesforce apex-code

我正在测试我的应用。它给了我错误

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.

我正在尝试调用Batch apex类,此应用程序基于Google Calendar Api.thats为什么json响应。但我面临上述错误无法解决。

@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();


    }

在完成方法完成批处理请求后,我再调用一个Batch class.can任何一个请指出我为什么我在testmethod中调用一个Batch类时出现此错误?请一些帮助

1 个答案:

答案 0 :(得分:2)

虽然您可以从另一个批次类调用批次类,但您只能从Test类中调用一个批次类。因此,您将面临此错误。

替代方法是检查调用Batch类的finish方法,无论您是在测试上下文还是常规上下文中。您可以使用Test.isRunningTest()执行此操作。