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
Vigneshwaran LoganathanVigneshwaran Loganathan 

Need halp in Test class

Hi guyz,

I have written a Test class for Batch clas with the help of google. but I could able to cover only 14%. In debug log My Execute method was not covered. Any help?

My Class :
global class batchVATenureUpdate implements Database.Batchable<AggregateResult>
{
    global Iterable<AggregateResult> start(Database.BatchableContext info)
    {
        return new AggregateVAtenureIterable();       
    }
    
    global void execute(Database.BatchableContext info,  List<AggregateResult> accva )
    {
        List<Our_VA__c> ourvalist = new List<Our_VA__c>();
        map<string,Decimal> mapva = new map<string,Decimal>();
        integer i;
        
        for(AggregateResult av : accva)
        {     
            mapva.put(string.valueof(av.get('VA__c')),(Decimal)av.get('daycount'));         
        }
        map<Id,Our_VA__c> ourva = new map<Id,Our_VA__c>();  
        List<Our_VA__c> ourva1 = new List<Our_VA__c>();
        ourva1 = [select id,VA_Tenure_Active_Clients__c,VA_Tenure_Cancellation__c,Account_Number__c,BonusCount__c,Name,ClientCount__c,LoanCount__c,test__c from Our_VA__c];// where id IN:vaids];
       
        for(Our_VA__c a : ourva1)
        {            
            ourva.put(a.id,a);
            string idva = string.valueof(a.id);
            if(mapva.containskey(idva) == true)
            {
                a.VA_Tenure_Active_Clients__c = mapva.get(idva);
                ourvalist.add(a);
            }
        } 
        update ourvalist;              
    }   
    global void finish(Database.BatchableContext BC)
    {
    }
}

Test class :
@isTest

public class TestvaTenureactiveclients1
{

 public static testmethod void batchVATenureUpdate()
    {
         
     our_va__c v1 = new our_va__c();
     v1.name = 'v1';
     insert v1;
     
     Account_VA__c accva = new Account_VA__c();
     accva.Account__c = a1.id;
     accva.VA__c = v1.id;
     accva.Status__c = 'Active';
     accva.Type__c = 'Full Time';
     accva.VA_start_Date__c = Null;
     insert accva;
     
     Test.StartTest();
     list<account_va__c> accvax=new list<account_va__c>();
     accvax.add(accva);
     upsert accvax;
     
     list<our_va__c> ourvax=new list<our_va__c>();
     ourvax.add(v1);
     upsert ourvax;    
     
     batchVATenureUpdate objBatch = new batchVATenureUpdate();
     Database.executeBatch(objBatch);
     Test.StopTest();
    }
}

Thanks,
ManojjenaManojjena
Hi Vighneswar,

AggregateVAtenureIterable is what you are returning from your start methos ,where is that code ??
 
Vigneshwaran LoganathanVigneshwaran Loganathan

Hi Manoj,

Its a Class that I am using to get values with Group by method..

Plz see below.

 

global class AggregateVAtenureIterable implements Iterable<AggregateResult> {
   global Iterator<AggregateResult> Iterator(){
      return new AggregateVATenureIterator();
   }
}
 
global class AggregateVATenureIterator implements Iterator<AggregateResult> {
   AggregateResult [] results {get;set;}
   // tracks which result item is returned
   Integer index {get; set;} 
   
   global AggregateVATenureIterator() {
      index = 0;
      Account_VA__c myVariable = new Account_VA__c(Status__c = 'Active',Account__c = '001a000001VtmwW',VA_start_Date__c = Null);
        string ids = myVariable.Account__c;
        string status = myVariable.Status__c;
        date sdate = myVariable.VA_start_Date__c;            
        String query = 'select sum(VAtenurecount__c)daycount,VA__c from Account_VA__c where Status__c =:status and Account__c !=:ids and VA_start_Date__c!=:sdate group by VA__c';
        results = Database.query(query);           
   } 
   
   global boolean hasNext(){ 
      return results != null && !results.isEmpty() && index < results.size(); 
   }    
   
   global AggregateResult next(){        
      return results[index++];            
   }       
}
Thanks for youre help. :)
 
Mari DanaMari Dana
Hi @Vigneshwaran,

Did you get any solution for this. I am facing the same problem.