Creating Test Classes on Apex Batch Class

2k views Asked by At

I am having hard time creating test classes for my Apex Batch Class SForecastBatchClass.

when creating my test class SForecastBatchClass_Test and saving it this error appears.

(SForecastBatchClass_Test) Constructor not defined: [SForecastBatchClass].(String)

Anyone can figure this out? hope I'm on the right page. Thanks.

Class:

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

    }
}

Test Class:

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

There are 1 answers

0
Kyle Olson On BEST ANSWER

You need to create a constructor for your class that accepts the SQL string. Then change your start() method so that it uses the string passed to the constructor.

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

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