+ Start a Discussion
kdaviskdavis 

Batch Apex Test Method Fails

Hi,

 

I'm recieving this error: 

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.

 

The scope is an iterable list of XML docs returned by the start method:

global Iterable<SyncXmlFeedsUtils.XmlFeed> start(Database.BatchableContext bc) {
return SyncXmlFeedsUtils.feeds;
}

 

the feeds list is formatted as such:

 

static {

feeds = new List<XmlFeed> {
new XmlFeed('URL----', PressReleaseParser.class),
new XmlFeed('URL----', AnalystReportsParser.class),
new XmlFeed('URL----', WebinarsParser.class),
new XmlFeed('URL----', ClientSuccessStoriesParser.class)
};
}

 

for my execute method I have:

global void execute(Database.BatchableContext bc, List<SyncXmlFeedsUtils.XmlFeed> scope) 

 

And then then the test class:

 

global class SyncXmlBatchTest {


@isTest static void testBatch() {


test.startTest();

Test.setMock(HttpCalloutMock.class, new HttpResonseGenerator());


List<RSINews__c> response = SyncXmlFeedsUtils.parseXmlFeeds(SyncXmlFeedsUtils.feeds);

 

SyncXmlFeedsBatchable batchTest = new SyncXmlFeedsBatchable ();


Id batchprocessId = Database.executeBatch(batchTest, 1);

test.stopTest();

}


}

 

 

Any Idea how to get around this error? If I don't set the batch size to 1 I get another error saying "System.CalloutException: . Please commit or rollback before calling out".

 

Any help would be very appreciated.

Thanks.

sfdcfoxsfdcfox
You can't have any DML operations performed when you try the callout. You must callout before attempting to insert, update, delete, or undelete records. Since you didn't post the contents of the batchable class, I'm not sure what's going on here. Check the batchable class first to make sure there are no DML operations in your execute function before the callouts.
kdaviskdavis

sfdcfox,

 

thank you for the response, sorry for not posting all the code I was trying to kind of break it up and make it a quick read. However, it turns out I was attempting to make 4 callouts per execute and I understand that the limit is one per execute. And inside my execute I was performing an upsert which I imagine was conflicting with the multiple callouts.