+ Start a Discussion
Nagarjuna Reddy.PNagarjuna Reddy.P 

Test Class for batch apex with If else if statements

Hi All,
my test class for batch apex is not covering the complete code,could you pls help me for complete coverage.
Here is my batch class

 global with sharing class batchHandler implements Database.Batchable<SObject> {
 global Database.QueryLocator start(Database.BatchableContext bc){
      String squery = 'SELECT Id,Referral_Stage__c,Account_Type__c,(SELECT Id,createdDate from child1__r ORDER BY CreatedDate DESC LIMIT 50),(SELECT Id,createdDate from child2__r  ORDER BY CreatedDate DESC LIMIT 50),(SELECT Id,createdDate from child3__r ORDER BY CreatedDate DESC LIMIT 50),(SELECT Id,createdDate from child4__r ORDER BY CreatedDate DESC LIMIT 50) from Account';
      return Database.getQueryLocator(squery);
  }
  global void execute(Database.BatchableContext bc,List<Account> scope){
    Date today = Date.today();
    Date twoMonthsBefore = today.addDays(-10);
    Set<Account> accounts = new Set<Account>();
    List<Account> accToUpdate = new List<Account>();
    // Map<id,Account> accMap=new Map<id,Account>();
    //if(scope.size()>0){
  for(Account acc:scope){
      try{
         if(!acc.child1__r .isEmpty()){
        for(Referral__c insurance:acc.child1__r ){
       // if(insurance.size()>0){
            if(insurance.createdDate > twoMonthsBefore){
            acc.Referral_Stage__c='Currently Referring';
            accounts.add(acc);
            //  accMap.put(acc.Id,acc);
           } else if(insurance.createdDate < twoMonthsBefore){
              acc.Referral_Stage__c='Referred Inactive';
              accounts.add(acc);
              //  accMap.put(acc.Id,acc);
           }
       //  }
           } 
         }else if(!acc.child2__r .isEmpty()){
             for(Referral__c mso:acc.child2__r ){
        if(mso.createdDate > twoMonthsBefore){
          acc.Referral_Stage__c='Currently Referring';
          accounts.add(acc);
          //  accMap.put(acc.Id,acc);
      }else if(mso.createdDate < twoMonthsBefore){
        acc.Referral_Stage__c='Referred Inactive';
        accounts.add(acc);
        //  accMap.put(acc.Id,acc);
       }
     }
      }else if(!acc.child3__r .isEmpty()){
          for(Referral__c ipa:acc.child3__r ){
        if(ipa.createdDate > twoMonthsBefore){
          acc.Referral_Stage__c='Currently Referring';
          accounts.add(acc);
          //  accMap.put(acc.Id,acc);
      }else if(ipa.createdDate < twoMonthsBefore){
        acc.Referral_Stage__c='Referred Inactive';
        accounts.add(acc);
        //  accMap.put(acc.Id,acc);
          }         
       }
      }else if(!acc.child4__r .isEmpty()){
          for(Referral__c pcp:acc.child4__r ){
        if(pcp.createdDate > twoMonthsBefore){
          acc.Referral_Stage__c='Currently Referring';
          accounts.add(acc);
          //  accMap.put(acc.Id,acc);
      }else if(pcp.createdDate < twoMonthsBefore){
        acc.Referral_Stage__c='Referred Inactive';
        accounts.add(acc);
        //  accMap.put(acc.Id,acc);
       }
      }
    }
      else{
          acc.Referral_Stage__c='Never Referred';
          accounts.add(acc);
          //  accMap.put(acc.Id,acc);
        }
      }catch(Exception e){
        }
      }
    
 // }
    if(!accounts.isEmpty()){
        //update accounts;
        accToUpdate.addAll(accounts);
    } 
      if(!accToUpdate.isEmpty()){
           update accToUpdate;
      }
      /*  if(accMap!=null){
            accToUpdate=accMap.values();
            //update accMap.values();
        }
        if(!accToUpdate.isEmpty()){
            update accToUpdate;
        } */ 
  }
  global void finish(Database.BatchableContext bc){
  }
}

My Test class is : 
 @isTest
private class batchHandlerTest {
    static testmethod void testBatch() {
         List<Account> acclist = new List<Account>();
         for(Integer i=0;i<200;i++){
           //  try{
             Account acc=new Account();
             acc.Name = 'Account'+i;
             acc.Account_Type__c = 'IPA';
             acc.Phone = '1234567890';
             acc.Referral_Stage__c = 'Never Referred';
             acclist.add(acc);
        // }catch(Exception e){}
            
           
        }
         insert acclist;
         Test.startTest();
           batchHandler b = new batchHandler();
           Database.executeBatch(b);
         Test.stopTest();
         Account[] updateRefStage = [select id,name,Referral_Stage__c from Account];
         system.assert(updateRefStage[0].Referral_Stage__c.contains('Never Referred'));
    }
}

This giving only 50% of coverage and If --- else if -- conditions are not covered. 
 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Nagarjuna,

You will have to write the test method so that there are different scenarios that are covered according to your code implementation.

I hope this helps.

Regards,
Anutej
Nagarjuna Reddy.PNagarjuna Reddy.P
Hi Anutej,
Thanks for your reply,could you please give me sample test method that covers atleast one if statement of my batch class. 
Nagarjuna Reddy.PNagarjuna Reddy.P
Hi All,
Could you please help me with test class for this Batchapex.
Thanks!!