Skip to main content
Arpita Kaushik preguntó en #Apex
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

 
3 respuestas
  1. 8 nov 2016, 17:41
    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

     
0/9000