Scheduled batch process in salesforce

I am trying to implement a batch process. I need help / guidance on testing. All I am doing here is to show the name of the opportunity in the debug logs. But when I run the scheduleBatchable class, which also has a test class in it when doing the Apex test. Debug operations are not displayed in Opp_BatchProcess. What am I 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 planned 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();
    }
}

      

+3


source to share


1 answer


It looks like your test method is only checking the Schedulable class. You also need to check the Batchable class.

Try the following:



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();
    }
}

      

+3


source







All Articles