function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arpita Kaushik 5Arpita Kaushik 5 

test class not covering execute method in batch Apex class

I have the following Batch job :

global class AdvisorGroupsBatch implements Database.Batchable<sObject>{
    
    public String query;
    
    global database.querylocator start(Database.BatchableContext BC)
  {
      String query = 'Select Id, Name, YTD_Gross_Adv_Group__c, SV_Assets_for_Team__c, (select Id, Name, YTD_Gross_Sales__c, Assets_with_WB__c from Contacts__r) from Advisor_group__c';
      return Database.getQueryLocator(query);
  }
   global void execute(Database.BatchableContext BC, List<sObject> scope)
  {
      decimal total_YTD;
      decimal total_SV;
      for (sObject obj: scope){
          total_YTD = 0;
          total_SV = 0;
          Advisor_group__c advgrp = (Advisor_group__c)obj;
          for (Contact c: advgrp.Contacts__r){
              total_YTD = total_YTD + c.YTD_Gross_Sales__c;
              total_SV = total_SV + c.Assets_with_WB__c;
          }
          advgrp.YTD_Gross_Adv_Group__c = total_YTD;
          advgrp.SV_Assets_for_Team__c = total_SV;
          update advgrp;
        
      }
  } 
  
  global void finish(Database.BatchableContext BC)
  {
    System.debug('Advisor Groups Batch finished.');
  }
    
}


I am trying to write a test class, following is the test class :
@isTest
private class AdvisorGroupsBatchTest {
    public static testMethod void test(){
       
       String query = 'Select Id, Name, YTD_Gross_Adv_Group__c, SV_Assets_for_Team__c, (select Id, Name, YTD_Gross_Sales__c, Assets_with_WB__c from Contacts__r) from Advisor_group__c LIMIT 2';
    
     Advisor_Group__c[] adv = new List<Advisor_Group__c>();
     
           Advisor_Group__c a1 = new Advisor_Group__c(
               Name='Adv Group',
               Id='000',
               YTD_Gross_Adv_Group__c=100,
               SV_Assets_for_Team__c=200);
 
           adv.add(a1);
     
     insert adv;
     
     Test.startTest();
     AdvisorGroupsBatch a = new AdvisorGroupsBatch(query);
      Database.executeBatch(a);
     //ID batchprocessid = Database.executeBatch(a);
     Test.stopTest();
  } 
                      
}

I am unable to reach a code coverage of 75%. it is not increasing more than 24%. Please suggest

 
Best Answer chosen by Arpita Kaushik 5
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for Batch job related query
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html


Update your test class like below
@isTest
public class AdvisorGroupsBatchTest
{
    static testMethod void testMethod1()
    {
	
		List<Advisor_Group__c> Listadv = new List<Advisor_Group__c>();
        // Add all required field for Advisor_Group__c
        Advisor_Group__c a1 = new Advisor_Group__c(
               Name='Adv Group',
               YTD_Gross_Adv_Group__c=100,
               SV_Assets_for_Team__c=200);
			   
		adv.add(a1);
		insert adv;
       
        Test.startTest();

            AdvisorGroupsBatch  obj = new AdvisorGroupsBatch ();
            DataBase.executeBatch(obj);
           
        Test.stopTest();
    }
}

Let us know if this will help you
 

All Answers

Jim JamJim Jam
You need a constructor in your batch class...something like ...

global AdvisorGroupsBatch(string q){
     query = q;
}

You then need to remove ...  String query = 'Select Id, Name, YTD_Gross_Adv_Group__c, SV_Assets_for_Team__c, (select Id, Name, YTD_Gross_Sales__c, Assets_with_WB__c from Contacts__r) from Advisor_group__c';

from the start method, and instead pass in the query string as a parameter when calling the batch class.

Your test class should be something like ...

Test.startTest();
Database.executeBatch(new AdvisorGroupsBatch(query), 10); //for batch size of 10, for example
Test.stopTest();
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for Batch job related query
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html


Update your test class like below
@isTest
public class AdvisorGroupsBatchTest
{
    static testMethod void testMethod1()
    {
	
		List<Advisor_Group__c> Listadv = new List<Advisor_Group__c>();
        // Add all required field for Advisor_Group__c
        Advisor_Group__c a1 = new Advisor_Group__c(
               Name='Adv Group',
               YTD_Gross_Adv_Group__c=100,
               SV_Assets_for_Team__c=200);
			   
		adv.add(a1);
		insert adv;
       
        Test.startTest();

            AdvisorGroupsBatch  obj = new AdvisorGroupsBatch ();
            DataBase.executeBatch(obj);
           
        Test.stopTest();
    }
}

Let us know if this will help you
 
This was selected as the best answer
Arpita Kaushik 5Arpita Kaushik 5
Thanks a lot the test class provided above worked :)