4

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

1 回答 1

4

看起来您的 testmethod 只测试 Schedulable 类。您还需要测试 Batchable 类。

尝试这个:

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();
    }
}
于 2012-04-09T21:02:52.543 回答