You need to sign in to do that
Don't have an account?
Zibing Wang
Batch Apex Testing class exception
Hi All,
I am trying to complete a task in Trailhead. When I run the testing class, I got this error 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.
I already limited the size in the query, so not sure why I am still getting the error.
I am trying to complete a task in Trailhead. When I run the testing class, I got this error 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.
global class LeadProcessor implements Database.Batchable<Sobject> { global Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator('SELECT ID, Name from Lead Limit 200'); } global void execute(Database.BatchableContext bc, List<Lead> scope) { for (Lead Leads : scope) { Leads.LeadSource = 'Dreamforce'; } update scope; } global void finish(Database.BatchableContext bc){ } }
@isTest public class LeadProcessorTest { static testMethod void testMethod1() { List<Lead> lstLead = new List<Lead>(); for(Integer i=0 ;i <200;i++) { Lead led = new Lead(); led.FirstName ='FirstName'; led.LastName ='LastName'+i; led.Company = 'test'+i; lstLead.add(led); } insert lstLead; Test.startTest(); LeadProcessor obj = new LeadProcessor(); DataBase.executeBatch(obj); Test.stopTest(); } }
I already limited the size in the query, so not sure why I am still getting the error.
Please modify batch class like this.
and test class
Let me know if it helps.
Make it as best answer if it helps.
Thanks.
All Answers
Please modify batch class like this.
and test class
Let me know if it helps.
Make it as best answer if it helps.
Thanks.