Salesforce 在Apex批处理类上创建测试类

Salesforce 在Apex批处理类上创建测试类,salesforce,apex-code,apex,Salesforce,Apex Code,Apex,我很难为我的Apex批处理类创建测试类 创建我的测试类并保存时,会出现此错误 (SForecastBatchClass_测试)未定义构造函数:[SForecastBatchClass]。(字符串) 有人能弄明白吗?希望我在正确的页面上。谢谢 类别: global class SForecastBatchClass implements Database.Batchable<SObject> { String soql; global Database.Q

我很难为我的Apex批处理类创建测试类

创建我的测试类并保存时,会出现此错误

(SForecastBatchClass_测试)未定义构造函数:[SForecastBatchClass]。(字符串)

有人能弄明白吗?希望我在正确的页面上。谢谢

类别:

    global class SForecastBatchClass implements Database.Batchable<SObject> {

    String soql;


    global Database.QueryLocator start(Database.BatchableContext BC) {

        soql = 'Select Salesperson__c, Quarter_Start__c, Quarter_End__c, Product__c, Sales_Forecast__c.Product__r.Category__c from Sales_Forecast__c WHERE Product__c!=null';
        return Database.getQueryLocator(soql);
    }

    global void execute(Database.BatchableContext BC, List<Sales_Forecast__c> salesForecastList) {
        Set<Id> ownerIds = new Set<Id>();
        Set<String> prodCategories = new Set<String>(); 

        for(Sales_Forecast__c sF: salesForecastList){
            ownerIds.add(sF.Salesperson__c);
            prodCategories.add(sF.Product__r.Category__c);
        }


        AggregateResult[] aList = [Select Opportunity.CloseDate, PricebookEntry.Product2.Category__c, Opportunity.OwnerId,
                                   SUM(One_Time_Amount__c)oneTime, SUM(Renewal_Amount__c)renewAmount, 
                                   SUM(Downsale_Amount__c)downSale, SUM(New_Amount__c)newAmount FROM OpportunityLineItem 
                                   WHERE Opportunity.OwnerId IN: ownerIds AND PricebookEntry.Product2.Category__c IN: prodCategories 
                                   GROUP BY PricebookEntry.Product2.Category__c, Opportunity.CloseDate, Opportunity.OwnerId];

        system.debug('*** aList = '+ aList);

        List<Sales_Forecast__c> salesForecastForUpdate = new List<Sales_Forecast__c>();
        for(Sales_Forecast__c sF: salesForecastList){

            Sales_Forecast__c tempSF = sF;

            system.debug('*** tempSF = '+ tempSF);

            for(AggregateResult ar : aList){
                if(sF.Product__r.Category__c == (String)ar.get('Category__c') 
                   && sF.Salesperson__c == (String)ar.get('OwnerId')
                   && (Date)ar.get('CloseDate') <= sF.Quarter_End__c 
                   && (Date)ar.get('CloseDate') >= sF.Quarter_Start__c){
                       system.debug('*** ar = '+ ar);
                       tempSF.One_Time_Actual__c = (Decimal)ar.get('oneTime');
                       tempSF.Renewal_Actual__c = (Decimal)ar.get('renewAmount');
                       tempSF.Cancellation_Loss_ACV_Actual__c = (Decimal)ar.get('downSale');
                       tempSF.New_Recurring_Actual__c = (Decimal)ar.get('newAmount');
                       salesForecastForUpdate.add(tempSF);
                   }

            }
        }
            system.debug('***salesForecastForUpdate: '+salesForecastForUpdate);


        update salesForecastForUpdate;


    }

    global void finish(Database.BatchableContext BC) {

    }
}

您需要为接受SQL字符串的类创建构造函数。然后更改start()方法,使其使用传递给构造函数的字符串

global SForecastBatchClass(String query) {
    this.soql = query;
}

global Database.QueryLocator start(Database.BatchableContext BC) {
    return Database.getQueryLocator(soql);
}
global SForecastBatchClass(String query) {
    this.soql = query;
}

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