salesforce中的预定Batchable流程

时间:2012-04-09 04:28:33

标签: salesforce apex-code

我正在尝试实施批处理。我需要一些关于如何测试的帮助/指导。我在这里所做的就是在调试日志中显示商机名称。但是当我运行class scheduledBatchable时,它在Apex测试执行中也有测试类。 Opp_BatchProcess上的调试语句未显示。我做错了什么?

这是我的代码

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) {}
}

我还有一个可调度的课程

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

1 个答案:

答案 0 :(得分: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();
    }
}