1

ia 测试我的应用程序。它给了我错误

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.

当此应用程序基于 Google Calendar Api 时,我正在尝试调用 Batch apex 类。这就是 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();


    }

在完成方法中完成批处理请求后,我又调用了一个批处理类。谁能指出我为什么在测试方法中调用一个批处理类时出现此错误?请帮忙

4

1 回答 1

2

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. You can do this using Test.isRunningTest().

于 2013-03-28T05:30:38.397 回答