Batch Class:
global class AccountBatch implements Database.Batchable<sObject> { global Database.QueryLocator start(Database.BatchableContext BC){ String query = 'SELECT Id, Name, Industry FROM Account'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<Account> accList){ for(Account acc : accList){ acc.Industry = 'Banking'; } update accList; } global void finish(Database.BatchableContext BC) { } }
Schedulable Class:
global class AccountBatchScheduled implements Schedulable { global void execute(SchedulableContext sc) { AccountBatch objBatch = new AccountBatch(); Database.executebatch(objBatch); } }
Test Class:
@isTest private class AccountBatchScheduledTest{ static testmethod void schedulerTest(){ String cronexpression = '0 0 0 15 3 ? *'; Account acc = new Account(); acc.Name = 'Test Account'; Insert acc; Test.startTest(); String jobId = System.schedule('ScheduleBatchApexClass', cronexpression, new AccountBatchScheduled()); CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId]; System.assertEquals(cronexpression, ct.CronExpression); System.assertEquals(0, ct.TimesTriggered); Test.stopTest(); } }