You need to sign in to do that
Don't have an account?
Maf_007
Apex scheduler calling batch test class
Hi All,
I am trying to write a test class for apex scheduler and batch class but I am not getting enough code coverage. I get enough coverage for batch class but not for the scheduler. My classes and text classes are following:
Scheduler Class:
global class WebserviceScheduler implements Schedulable { global WebserviceScheduler(){} public static void start(){ //Seconds Minutes Hours Day_of_month Month Day_of_week optional_year System.schedule('Pending Cost Score Update', '0 5 1-23 * * ?', New WebserviceScheduler()); } global void execute(SchedulableContext SC) { WebserviceBatchHandler bh = new WebserviceBatchHandler(); database.executebatch(bh,10); } }
Batch Class:
global class WebserviceBatchHandler implements Database.Batchable<Me__c>, Database.AllowsCallouts{ Me__c[] msquery = [SELECT Id,Name,Annual_Saving__c,Cost__c FROM Me__c where Cost__c =: null]; global Iterable<Me__c> start(Database.BatchableContext BC){ system.debug(msquery); } global void execute(Database.BatchableContext BC, List<Me__c> scope){ // for(Me__c MStoupdate : scope){ String MSName = MStoupdate.Name; CallNesWS ws = new CallNesWS(); ws.CallNesWS(MSName); } //Pass the opportunities to web service method //CallNesWS.CallNesWSMethod(oppmap,oppid); } global void finish(Database.BatchableContext BC) { //Send an email to the User after your batch completes Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'maf@xxx.com'}; mail.setToAddresses(toAddresses); mail.setSubject('Apex Batch Synch Job is done'); mail.setPlainTextBody('The batch Apex Synch job processed '); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } }
And Test Class is below:
@isTest global class TestBatchScheduler { public static testMethod void testscheduleClass() { List<Opportunity>oplist = new List<Opportunity>{}; SchedulableContext SC; Test.startTest(); Account a = new Account(Name = 'Maf Test'); insert a; Opportunity opp = new Opportunity (Name='testopp', Accountid = a.id,stageName = 'request result'); oplist.add(opp); Me__c ms = new Me__c(Annual_Saving__c = 100); insert ms; WebserviceScheduler wsch = new WebserviceScheduler(); wsch.execute(SC); string schedule = '0 5 1-23 * * ?'; system.schedule('Process Trans 1', schedule, wsch); Test.stopTest(); } public static testMethod void testBatchClass() { Database.BatchableContext BC; Test.startTest(); WebserviceBatchHandler b = new WebserviceBatchHandler(); ID myBatchJobID = database.executebatch(b); Test.stopTest(); } }
Any idea how I can increase code coverage for Scheduler???
Also How can I test the batch class from debug window as I get invalid batchablecontext error??