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
Zibing WangZibing 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.
 
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. 

 
Best Answer chosen by Zibing Wang
Dilip_VDilip_V
Hi wang,

Please modify batch class like this.
 
global class LeadProcessor implements    Database.Batchable<Sobject> 
{
    global Database.QueryLocator start(Database.BatchableContext bc) 
    {
        return Database.getQueryLocator([Select LeadSource From Lead ]);
    }

    global void execute(Database.BatchableContext bc, List<Lead> scope)
    {
            for (Lead Leads : scope) 
            {
                Leads.LeadSource = 'Dreamforce';
            }
        update scope;
    }    

    global void finish(Database.BatchableContext bc){   }    
}

and test class 
@istest
public class LeadProcessorTest {
    @istest
    Public static void MyTestMethod()
    {
        
        try{
            List<Lead> Leads=new List<Lead>();
            For(integer i=1;i<=10;i++)
            {
                Lead L=new Lead();
                L.LastName='Test'+i;
                L.LeadSource='Dreamforce';
                L.Company='SF';
                L.Status='Working - Contacted';
                Leads.add(L);
            }
            Insert Leads;
            
            Test.startTest();
            LeadProcessor uca = new LeadProcessor();
            Id batchId = Database.executeBatch(uca);
            Test.stopTest();
        }
        Catch(Exception e)
        {
            system.debug('Exception:'+e+e.getLineNumber());
        }
    }
    
}

Let me know if it helps.

Make it as best answer if it helps.

Thanks.

All Answers

Zibing WangZibing Wang
Updated batch class here
global class LeadProcessor implements    Database.Batchable<Sobject> 
{
    global Database.QueryLocator start(Database.BatchableContext bc) 
    {
        return Database.getQueryLocator('SELECT ID, LeadSource 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){   }    
}

 
Dilip_VDilip_V
Hi wang,

Please modify batch class like this.
 
global class LeadProcessor implements    Database.Batchable<Sobject> 
{
    global Database.QueryLocator start(Database.BatchableContext bc) 
    {
        return Database.getQueryLocator([Select LeadSource From Lead ]);
    }

    global void execute(Database.BatchableContext bc, List<Lead> scope)
    {
            for (Lead Leads : scope) 
            {
                Leads.LeadSource = 'Dreamforce';
            }
        update scope;
    }    

    global void finish(Database.BatchableContext bc){   }    
}

and test class 
@istest
public class LeadProcessorTest {
    @istest
    Public static void MyTestMethod()
    {
        
        try{
            List<Lead> Leads=new List<Lead>();
            For(integer i=1;i<=10;i++)
            {
                Lead L=new Lead();
                L.LastName='Test'+i;
                L.LeadSource='Dreamforce';
                L.Company='SF';
                L.Status='Working - Contacted';
                Leads.add(L);
            }
            Insert Leads;
            
            Test.startTest();
            LeadProcessor uca = new LeadProcessor();
            Id batchId = Database.executeBatch(uca);
            Test.stopTest();
        }
        Catch(Exception e)
        {
            system.debug('Exception:'+e+e.getLineNumber());
        }
    }
    
}

Let me know if it helps.

Make it as best answer if it helps.

Thanks.
This was selected as the best answer
Zibing WangZibing Wang
Thank you Dilip, I already made it the best answer. Just want to ask why the size in the loop of the test class has to be 10? I changed to 200 and it started complaining.