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

Test Class for "Schedulable interface with batch Apex classes"

Need to get code coverage .Test class is getting passed. But it is not calling ther method
global void execute(Database.BatchableContext bc, List<case> caselist) ". Coverage is only 45%.

Please suggest me , how to get the 75%%coverage..

global class  BatchUpdateCase implements Schedulable, Database.Batchable<sObject> {
    public void execute(SchedulableContext ctx) {

    public static void run() {
        Database.executeBatch(new BatchUpdateCase());

     global Database.QueryLocator start(Database.BatchableContext bc)
        return Database.getQueryLocator([select id,SuppliedEmail__c from Case Where SuppliedEmail__c = '' AND Test_Record__c = false]); 

global void execute(Database.BatchableContext bc, List<case> caselist){
      for(case cas:caselist)
      System.debug('### Test Record Updated');
       update caselist;
    global void finish(Database.BatchableContext BC) {

Test Class:

public class  batchUpdateCaseTestClass {

    private static testMethod void testBatch() {
        Case cas=new Case();
        cas.SuppliedEmail__c = '' ;
        cas.Test_Record__c = false;

            insert cas;




Mariappan PerumalMariappan Perumal
Hi ,

First of all you should write the test class for controller which implements the schedulable interface which will coverage the batch class in your case .

Try the below snippe inside the test class 's testmethods :

ScheduleUpdateAccountStatus SUA = new ScheduleUpdateAccountStatus();
String sch = '0 0 23 * * ?';
system.schedule('Test Check', sch, SUA);

kindly let me know incase you could find the solution after trying this snippet.

Hi, Thanks for your reply.

Actually . i checked with Debug log,
The query is not returning any values.

I could not find anything wrong in my test data.

Is that somewhere am   i missing?
Mariappan PerumalMariappan Perumal

Actually you can't call the run method directly  Schedule class calls the execute method by default and then it acutomaically call your run method.

I am not sure we can implement both schedulabe and batchable in the single controller . Separate that into 2 different class and give a try . 

and there is a problem in this area too. 
List<Case> casetoupdate=new List<Case>();
for(case cas:caselist)
//Add the case to list and update out side of loop.
       update caselist;

 Kindly let me know incase the issue is not resolve
Just call the other methods directly.  Add this to your test:

BatchUpdateCase theBatch = new BatchUpdateCase();
theBatch.start( (Database.BatchableContext) null );
theBatch.execute( (Database.BatchableContext) null, new List<Case>{ cas } );
theBatch.finish( (Database.BatchableContext) null );