【问题标题】:Creating a test Class for batch apex为批处理顶点创建测试类
【发布时间】:2015-01-14 09:42:34
【问题描述】:

我在为这个特定的类创建一个测试类时遇到了麻烦。如果有人能提供一些可以实现这一点的代码,我将不胜感激。

非常感谢

类:

global class TalentIntCustomerBatch implements Database.Batchable<sObject>, Database.AllowsCallouts{
    global final String query;

    global TalentIntCustomerBatch(String q){
        query=q;
    }

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope){     
        for(sObject s : scope){
            Contact c = (Contact)s;
            TalentIntegrationUtils.updateCustomer(c.Id, c.LastName);
        }
    }

    global void finish(Database.BatchableContext BC){}
}

【问题讨论】:

    标签: salesforce apex-code visualforce apex test-class


    【解决方案1】:

    您需要在测试中填充数据以创建联系人和 TalentIntegrationUtils 类所需的任何其他对象,但以下代码应该可以测试它:

    string query = 'Select Id, LastName From Contact';
    TalentIntCustomerBatch ticb = new TalentIntCustomerBatch(query);
    Database.executeBatch(ticb);
    

    根据您的班级名称,您可能会在测试期间调用外部系统。如果是这种情况,您需要在所有调用周围添加“if (Test.isRunningTest() == false)”块或实现模拟响应:

    Testing Web Service Callouts

    Testing HTTP Callouts by Implementing the HttpCalloutMock Interface

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 2021-10-16
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 2020-10-24
      相关资源
      最近更新 更多