+ Start a Discussion
Jyosi jyosiJyosi jyosi 

Needed help on Test Coverage for batch Class

Hello Everyone,

I am reallly struck to get the Test coverage for the batch calls,Can you please help me out
/*
 Purpose: Creates an Adjustment Record Based on Techonlogy
Will Update the logic here
 
 */
global class AdjustmentFeeBatch implements Database.Batchable<sObject> 
 {
       global Database.QueryLocator start(Database.BatchableContext BC) {
       String query = 'Select Id,Name,Contact__c,End_Date__c,Name__c,Fee_frequency__c,Package__c,Start_Date__c,Waive_End_Date__c,Fee_Amount__c,Waive_Start_Date__c,Status__c,Waive_Amount__c from Technology__C';
       system.debug('query=====>>>>>>>>>>>>>>>>'+query); 
       return Database.getQueryLocator(query);
       
    }
  global void execute(Database.BatchableContext BC, List<Technology__C> scope) {
         List<Adjustment_Fees__c> AdjustmentFeeInsert= new List<Adjustment_Fees__c>();
     for(Technology__C Technology : scope)
      {
      //if(Technology.Id=='a0YK00000021I5b')
         //{  
              Datetime todayDate =date.today();
              system.debug('todayDate====>>>>'+todayDate);
              Datetime StartDate=Technology.Start_Date__c;
              Datetime EndDates=Technology.End_Date__c;
              Datetime WaivesEndDates=Technology.Waive_End_Date__c;
              Datetime WaiveStartDates=Technology.Waive_Start_Date__c;
              system.debug('WaiveStartDateValue==========>>>>>>>>>'+WaiveStartDates);
              Integer CurrentDay=todayDate.dayGmt();
              Integer currentMonth = Date.Today().Month();
              Integer startDay;
              Integer startDays;
          // set Start Date
          if(WaiveStartDates!=null){
               Date WaiveStdate=WaiveStartDates.dateGmt();
               startDay=WaiveStdate.day();
             }else{
              Date vStartDate=StartDate.dateGmt();
              startDays=vStartDate.day();
            }
          system.debug('startDayoutside===========>>>>>>'+startDay);
          //PACKAGE IS ACTIVE WITH OUT END DATE
         if(EndDates==null) //ITS CHECKS WHETEHR THE PACKAGE IS ACTIVE and havine waive date 
          {
              
             if(WaivesEndDates!=null || WaiveStartDates!=null)
              {  
                  //Integer WaiveEndMonth=WaivesEndDates.month();
                  system.debug('WaivesEndDates---->'+WaivesEndDates);
                  system.debug(WaivesEndDates+'ASHOK'+todayDate);
                  if(WaivesEndDates==null || WaivesEndDates>=todayDate)
                   {
                       system.debug('Technology------------3>>>>>>>>>>>>>> I am here ');
                       system.debug('Technology------------4>>>>>>>>>>>>>>'+Technology.Id);
                       system.debug('CurrentDay----5>>>>>>>>>>>>>>>'+CurrentDay+'>>>>>>>>>>'+startDay);
                      if(CurrentDay==startDay)
                      {
                          Adjustment_Fees__c Adjustment = new Adjustment_Fees__c();
                          Adjustment.Technologies__c=Technology.Id;
                          Adjustment.Contact__c=Technology.Contact__c;
                          Adjustment.Adjustment_Type__c='Technology Fee';
                          Adjustment.Frequency__c=Technology.Fee_frequency__c;
                          Adjustment.Adjustment_Description__c=Technology.Name__c +'--'+Technology.Package__c;
                          Adjustment.Start_date__c=Technology.Start_Date__c;
                          Adjustment.Waive_Start_Date__c=Technology.Waive_Start_Date__c;
                          Adjustment.Waive_End_Date__c=Technology.Waive_End_Date__c;
                          Adjustment.Amount__c=Technology.Waive_Amount__c;
                          AdjustmentFeeInsert.add(Adjustment);
                      }
                  
                  }else{
                            system.debug('FeeAmount===========>>>>>>'+Technology.Fee_Amount__c);
                            Date vStartDate=StartDate.dateGmt();
                            startDay=vStartDate.day();
                            system.debug(endDates+'JPMOR===========>>>>>>>>>>>>>>>>'+todayDate);
                           if(CurrentDay==startDay)
                          {
                              system.debug('Technology------------2>>>>>>>>>>>>>> I am here ');
                              Adjustment_Fees__c Adjustment = new Adjustment_Fees__c();
                              Adjustment.Technologies__c=Technology.Id;
                              Adjustment.Contact__c=Technology.Contact__c;
                              Adjustment.Adjustment_Type__c='Technology Fee';
                              Adjustment.Frequency__c=Technology.Fee_frequency__c;
                              Adjustment.Adjustment_Description__c=Technology.Name__c +'--'+Technology.Package__c;
                              Adjustment.Start_date__c=Technology.Start_Date__c;
                              Adjustment.Waive_Start_Date__c=Technology.Waive_Start_Date__c;
                              Adjustment.Waive_End_Date__c=Technology.Waive_End_Date__c;
                              Adjustment.Amount__c=Technology.Fee_Amount__c;
                              AdjustmentFeeInsert.add(Adjustment);
                        }
                   }
              }else 
                  {   
                      Date vStartDate=StartDate.dateGmt();
                        startDay=vStartDate.day();
                      startDay=StartDate.dayGmt();
                      if(startDay==CurrentDay)
                      {
                          system.debug('Technology------------1>>>>>>>>>>>>>> I am here ');
                          Adjustment_Fees__c Adjustment = new Adjustment_Fees__c();
                          Adjustment.Technologies__c=Technology.Id;
                          Adjustment.Contact__c=Technology.Contact__c;
                          Adjustment.Adjustment_Type__c='Technology Fee';
                          Adjustment.Frequency__c=Technology.Fee_frequency__c;
                          Adjustment.Adjustment_Description__c=Technology.Name__c +'--'+Technology.Package__c;
                          Adjustment.Start_date__c=Technology.Start_Date__c;
                          Adjustment.Waive_Start_Date__c=Technology.Waive_Start_Date__c;
                          Adjustment.Waive_End_Date__c=Technology.Waive_End_Date__c;
                          Adjustment.Amount__c=Technology.Fee_Amount__c;//Added Here
                          AdjustmentFeeInsert.add(Adjustment);
                      }
                  }
           }else {  //IF  BOTH END DATE IS NOT BLANK
             if(WaivesEndDates!=null || WaiveStartDates!=null)
              {
                  //Integer WaiveEndMonth=WaivesEndDates.month();
                 if(WaivesEndDates==null || WaivesEndDates>=todayDate)
                   {
                      if(CurrentDay==startDay)
                      {
                              system.debug('Technology------------>>>>>>>>>>>>>> I am here ');
                              Adjustment_Fees__c Adjustment = new Adjustment_Fees__c();
                              Adjustment.Technologies__c=Technology.Id;
                              Adjustment.Contact__c=Technology.Contact__c;
                              Adjustment.Adjustment_Type__c='Technology Fee';
                              Adjustment.Frequency__c=Technology.Fee_frequency__c;
                              Adjustment.Adjustment_Description__c=Technology.Name__c +'--'+Technology.Package__c;
                              Adjustment.Start_date__c=Technology.Start_Date__c;
                              Adjustment.Waive_Start_Date__c=Technology.Waive_Start_Date__c;
                              Adjustment.Waive_End_Date__c=Technology.Waive_End_Date__c;
                              Adjustment.Amount__c=Technology.Waive_Amount__c;
                              AdjustmentFeeInsert.add(Adjustment);
                      }
                   }else{
                       system.debug(endDates+'JPMOR===========121>>>>>>>>>>>>>>>>'+todayDate);
                      if(endDates>=todayDate)
                      {
                        if(CurrentDay==startDay)
                          {
                              system.debug('Technology------------121>>>>>>>>>>>>>> I am here ');
                              Adjustment_Fees__c Adjustment = new Adjustment_Fees__c();
                              Adjustment.Technologies__c=Technology.Id;
                              Adjustment.Contact__c=Technology.Contact__c;
                              Adjustment.Adjustment_Type__c='Technology Fee';
                              Adjustment.Frequency__c=Technology.Fee_frequency__c;
                              Adjustment.Adjustment_Description__c=Technology.Name__c +'--'+Technology.Package__c;
                              Adjustment.Start_date__c=Technology.Start_Date__c;
                              Adjustment.Waive_Start_Date__c=Technology.Waive_Start_Date__c;
                              Adjustment.Waive_End_Date__c=Technology.Waive_End_Date__c;
                              Adjustment.Amount__c=Technology.Waive_Amount__c;
                              AdjustmentFeeInsert.add(Adjustment);
                        }
                      }
                   }
              }else 
                  {
                      if(endDates>=todayDate)
                      {
                      if(startDay==CurrentDay)
                       {
                              system.debug('Technology------------>>>>>>>>>>>>>> I am here ');
                              Adjustment_Fees__c Adjustment = new Adjustment_Fees__c();
                              Adjustment.Technologies__c=Technology.Id;
                              Adjustment.Contact__c=Technology.Contact__c;
                              Adjustment.Adjustment_Type__c='Technology Fee';
                              Adjustment.Frequency__c=Technology.Fee_frequency__c;
                              Adjustment.Adjustment_Description__c=Technology.Name__c +''+Technology.Package__c;
                              Adjustment.Start_date__c=Technology.Start_Date__c;
                              Adjustment.Waive_Start_Date__c=Technology.Waive_Start_Date__c;
                              Adjustment.Waive_End_Date__c=Technology.Waive_End_Date__c;
                              Adjustment.Amount__c=Technology.Fee_Amount__c;//Added Here on 03/18
                              AdjustmentFeeInsert.add(Adjustment);
                        }
                      }
                  //}
          }
             //CODED HERE FOR 
             /*
             if(WaivesEndDates==null)
             {
                if(startDay==CurrentDay)
                       {
                          system.debug('Technology------------>>>>>>>>>>>>>> I am here ');
                          Adjustment_Fees__c Adjustment = new Adjustment_Fees__c();
                          Adjustment.Technologies__c=Technology.Id;
                          Adjustment.Contact__c=Technology.Contact__c;
                          Adjustment.Adjustment_Type__c='Technology Fee';
                          Adjustment.Frequency__c=Technology.Fee_frequency__c;
                          Adjustment.Adjustment_Description__c=Technology.Name__c +''+Technology.Package__c;
                          Adjustment.Start_date__c=Technology.Start_Date__c;
                          Adjustment.Waive_Start_Date__c=Technology.Waive_Start_Date__c;
                          Adjustment.Waive_End_Date__c=Technology.Waive_End_Date__c;
                          Adjustment.Amount__c=Technology.Waive_Amount__c;
                          AdjustmentFeeInsert.add(Adjustment);
                       } 
             }
*/
                    
            }
      
      }
      
     
     if(AdjustmentFeeInsert.size()>0)
    
           insert AdjustmentFeeInsert;
      }
    global void finish(Database.BatchableContext BC) {
        
    }
 }
Scott.EngScott.Eng
What does your current test class look like?

This might be helpful:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm#apex_batch_testing

A couple of things to keep in mind - the first line in the documentation states you can only test one execution of the Execute method.  Also, notice in the example code the reassign.query has a LIMIT 200 in it.  I believe that for testing, SF will only evaluate a single 200-size batch.  If your test data size is greater than 200, the Database.executeBatch won't work.
Jyosi jyosiJyosi jyosi
Sorry for the delay response.

Below is my Test Class

@isTest(SeeAllData=true)
public class AdjustmentFeeBatchTest {
    static testmethod void m1()
    {
       String query = 'Select Id,Name,Contact__c,End_Date__c,Name__c,Fee_frequency__c,Package__c,Start_Date__c,Waive_End_Date__c,Fee_Amount__c,Waive_Start_Date__c,Status__c,Waive_Amount__c from Technology__C';
       Adjustment_Fees__c[] Adjustment= new List<Adjustment_Fees__c>();
      Adjustment_Fees__c m = new Adjustment_Fees__c(Amount__c=20,Contact__c='003K000000wh1kC',Technologies__c='a0YK00000021NF9');
      Adjustment.add(m);
      insert Adjustment;
       Test.startTest();
       //AdjustmentFeeBatchScheduler c = new AdjustmentFeeBatchScheduler();
       //Database.executeBatch(c);
       Test.stopTest();

  }