+ Start a Discussion
Guru 91Guru 91 

Code Coverage for Batch class?

Hi,
need test class help

global class CDPForecastExtractBatch implements Database.Batchable<sObject> {
    
    String query;
    Forecast_Scenario__c forecastScenario;
    
    global CDPForecastExtractBatch(Forecast_Scenario__c scenario) {
        forecastScenario = scenario;
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        query = 'SELECT Id, Deal__c, Deal__r.Parent_Deal_Id__c, Deal__r.Stage__c, Deal__r.IsPlaceholder_Deal__c, Deal__r.IsInfrastructure_Deal__c, Deal__r.RecordType.Name, Deal__r.Is_Cloned_Deal__c, Deal__r.Is_Closed_Deal__c, Deal__r.Forecast_Scenario__c, ';
        query += '(SELECT Id, CDP_Asset__c, Customer__c, Customer__r.Name,Customer__r.Account_Subtype__c FROM CDP_Asset_Customer_Accounts__r) , ';
        query += '(SELECT Id, Development_Deal_Id__c, Land_Name__c, CDP_Asset__c, Land_Area__c, Land_Area_UOM__c, Land_Source__c, Land_Code__c FROM CDP_Land_Strategy__r) '; 
        query += 'FROM CDP_Asset__c WHERE (NOT Deal__r.Stage__c like \'%Lost%\')  AND (Deal__r.Is_Cloned_Deal__c = true OR (Deal__r.Stage__c like \'%Won%\') ) ';
        query += 'AND Deal__r.Hold__c = false AND (Deal__r.IsNamedDeal__c = true OR Deal__r.IsPlaceholder_Deal__c = true OR Deal__r.IsInfrastructure_Deal__c = true OR (Deal__r.Stage__c like \'%Won%\') ) ';
           query += 'AND Deal__r.RecordType.Name NOT IN ('+ '\'Value-Add\'' +')';
        return Database.getQueryLocator(query);
    }

       global void execute(Database.BatchableContext BC, List<sObject> scope) {
        CDPForecastExtractHandler.getChildRecords(scope, forecastScenario);
    }
    
    global void finish(Database.BatchableContext BC) {
        
    }
    
}
Raj VakatiRaj Vakati
Try this 
@istest
public class CDPForecastExtractBatchTest{

 @testSetup 
    static void setup() {
		
		Deal__c deal= new Deal__c();
		//Add all required fields 
insert deal ;		
		
		CDP_Asset_Customer_Accounts__c cdpCust= new CDP_Asset_Customer_Accounts__c();
		//Add all required fields 
		insert cdpCust ;
		
		
		CDP_Land_Strategy__c land = new CDP_Land_Strategy__c(); 
		//add all fields 
		insert land;
		
        List<CDP_Asset__c > assets = new List<CDP_Asset__c >();
         
        for (Integer i=0;i<10;i++) {
			////Add all required fields 
			// make sure this data setup matches to your soql 
            assets.add(new CDP_Asset__c (name='Account',RecordTypeId='' 
               ));
        }
        insert assets;
       
    }
    static testmethod void test() {        
        Test.startTest();
        CDPForecastExtractBatch  uca = new CDPForecastExtractBatch ();
        Id batchId = Database.executeBatch(uca);
        Test.stopTest();
       
    }


}