Scheduled Batchable process in salesforce

apex-code, salesforce

Solution

It looks like your testmethod only tests the Schedulable class. You need to test the Batchable class as well.

Try this:

global class scheduledBatchable implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        Opp_BatchProcess b = new Opp_BatchProcess();
        ID myBatchJobID = database.executebatch(b);
    }

    public static testMethod void testscheduleMerge()
    {
        Test.startTest();
        scheduledBatchable s8 = new scheduledBatchable();
        string sch = '0 0 * * 1-12 ? *';
        system.schedule('Process Trans 1', sch, s8);
        Test.stopTest();
    }

    public static testMethod void testBatchMerge()
    {
        Test.startTest();
        Opp_BatchProcess b = new Opp_BatchProcess();
        ID myBatchJobID = database.executebatch(b);
        Test.stopTest();
    }
}

Problem

I am trying my hands on implementing a batch process. I need some help/guidance on how to test this. All I am doing here is to display the opportunity name in the debug logs. But when I run the class scheduledBatchable which also has the test class in it in Apex test execution. The debug statements on the Opp_BatchProcess are not getting displayed. What is that I am doing wrong? Here is the code i have ``` global class Opp_BatchProcess implements Database.Batchable < sObject > { globalDatabase.QueryLocator start(Database.BatchableContextBC) { system.debug('Insidestart'); returnDatabase.getQueryLocator('select name,id from opportunity'); } global void execute(Database.BatchableContext BC, List <sObject> batch) { for (Sobject s : batch) { opportunity o = (opportunity)s; system.debug('Opp name is' + o.name); } } global void finish(Database.BatchableContext BC) {} } ``` I also have a schedulable class ``` global class scheduledBatchable implements Schedulable { global void execute(SchedulableContext sc) { Opp_BatchProcess b = new Opp_BatchProcess(); ID myBatchJobID = database.executebatch(b); } public static testMethod void testscheduleMerge() { Test.startTest(); scheduledBatchable s8 = new scheduledBatchable(); string sch = '0 0 * * 1-12 ? *'; system.schedule('Process Trans 1', sch, s8); Test.stopTest(); } } ```

Original source