我很难为我的Apex Batch Class SForecastBatchClass 创建测试类。
创建我的测试类 SForecastBatchClass_Test 并保存时会出现此错误。
(SForecastBatchClass_Test)未定义的构造函数:[SForecastBatchClass]。(String)
任何人都可以解决这个问题吗?希望我在正确的页面上。感谢。
类别:
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) {
}
}
测试类:
@isTest
private class SForecastBatchClass_Test {
@isTest static void testBatchClass() {
// Implement test code
Test.startTest();
String query = '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';
SForecastBatchClass sf = new SForecastBatchClass(query);
Database.executeBatch(sf);
Test.stopTest();
}
}
答案 0 :(得分:0)
您需要为接受SQL字符串的类创建构造函数。然后更改你的start()方法,使它使用传递给构造函数的字符串。
global SForecastBatchClass(String query) {
this.soql = query;
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(soql);
}