+ Start a Discussion
ezhil_kezhil_k 

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..
Class:


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

    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 = 'km@gmail.com' AND Test_Record__c = false]); 
    }

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



Test Class:

@isTest(SeeAllData=true)
public class  batchUpdateCaseTestClass {

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

            insert cas;

         Test.startTest();

        BatchUpdateCase.run();

        Test.stopTest();

     
      
    }

   
}
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.

ezhil_kezhil_k
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
Hi,

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)
      {
          cas.Test_Record__c=TRUE;
          casetoupdate.add(cas); 
//Add the case to list and update out side of loop.
      }
     
       update caselist;

 Kindly let me know incase the issue is not resolve
GlynAGlynA
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 );

-Glyn