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
Umamageshwari PalanisamyUmamageshwari Palanisamy 

how to write test class for aggreagate functions ?

I have written the test class for my apex class. The test class is not covered inside the aggregate functions. Please help me out !

My apex class:
public with sharing class Rfleet_DashboardcurrentYear{
public decimal Dvol{get;set;}
public decimal dElTarSum{get;set;}
public decimal tArRatioSum{get;set;}
public decimal dElComSum{get;set;}
public decimal cOmRatioSum{get;set;}
public decimal iKamForcastSum{get;set;}
public String mOnthName{get;set;}
public decimal cUrntMonth{get;set;}
public decimal cOunForcastSum{get;set;}
public decimal cOunForCastRatio{get;set;} 
public decimal cUrntYear{get;set;}
public String str{get;set;}
public String s{get;set;}
public String selectedval{get;set;}
public decimal iKamForcastRatio{get;set;} 
public decimal iKamForcastRatioIn;
public decimal cOunForCastRatioIn;
public decimal maxvalue{get;set;} 
public Rfleet_DashboardcurrentYear(ApexPages.StandardController controller) {
//Global deliveries Realized
    List<AggregateResult> tAr=[select SUM(Countries_delivered_volume__c) from IKAM_Data__c where Current_month__c=this_year group by CALENDAR_YEAR(Current_month__c)];
    for (AggregateResult ar : tAr)  {
    Dvol=(decimal)ar.get('expr0');
     }
// Annual global deliveries target   
    AggregateResult[] tArRatio=[select SUM(Target_ratio__c) from IKAM_Data__c where Current_month__c=this_year group by CALENDAR_YEAR(Current_month__c)];
    for (AggregateResult ar : tArRatio)  {
    Decimal tArRatioSumIn=(decimal) ar.get('expr0');
    tArRatioSum=tArRatioSumIn.setScale(0,System.Roundingmode.HALF_UP); 
    }

    AggregateResult[] dElTar=[select SUM(Annual_deliveries_target__c) from IKAM_Data__c where Current_month__c=this_year group by CALENDAR_YEAR(Current_month__c)];
    for (AggregateResult ar : dElTar)  {
    dElTarSum=(decimal) ar.get('expr0');
    //system.debug('<<<<<<<a value'+dElTarSum);
    }
//Annual global deliveries Commitment
    AggregateResult[] cOmRatio=[select SUM(Commitment_Ratio__c) from IKAM_Data__c where Current_month__c=this_year group by CALENDAR_YEAR(Current_month__c)];
    for (AggregateResult ar : cOmRatio)  {
    Decimal cOmRatioSumIn=(decimal) ar.get('expr0');
    //system.debug('<<<<<<<a value'+cOmRatioSumIn);
    if(cOmRatioSumIn!=null)
    cOmRatioSum=cOmRatioSumIn.setScale(0,System.Roundingmode.HALF_UP); 
    }


    AggregateResult[] dElCom=[select SUM(Annual_global_deliveries_commitment__c) from IKAM_Data__c where Current_month__c=this_year group by CALENDAR_YEAR(Current_month__c)];
    for (AggregateResult ar : dElCom)  {
    dElComSum=(decimal) ar.get('expr0');
    //system.debug('<<<<<<<a value'+dElComSum);
    }
//Country Forecast

    AggregateResult[] cOunForcast=[select SUM(Countries_forecast__c) from IKAM_Data__c where Current_month__c=this_year group by CALENDAR_YEAR(Current_month__c)];
    if(cOunForcast.size()>0)
    {
    for (AggregateResult ar : cOunForcast)  {
    cOunForcastSum=(decimal) ar.get('expr0');
    //system.debug('<<<<<<<a value'+cOunForcastSum);
    }
    }

    if(cOunForcastSum!=null)
    {
    cOunForCastRatioIn=(Dvol/cOunForcastSum)*100;
    cOunForCastRatio=cOunForCastRatioIn.setScale(0,System.Roundingmode.HALF_UP); 
    }

//Current year

    AggregateResult[] cUrntYr=[select CALENDAR_YEAR(Current_month__c) from IKAM_Data__c where Current_month__c=this_year group by CALENDAR_YEAR(Current_month__c)];
    for (AggregateResult ar : cUrntYr)  {
    cUrntYear=(Integer) ar.get('expr0');
    //system.debug('<<<<<<<a value'+cUrntYear);
    }
    str=String.valueof(cUrntYear);

//Current month
    AggregateResult[] cUrntMon=[select CALENDAR_MONTH(Current_month__c) from IKAM_Data__c where Current_month__c=this_year group by CALENDAR_MONTH(Current_month__c) order by CALENDAR_MONTH(Current_month__c) desc limit 1];
    for (AggregateResult ar : cUrntMon)  {
    cUrntMonth=(Integer) ar.get('expr0');
    //system.debug('<<<<<<<a value'+cUrntMonth);
    }

If(cUrntMonth==1){mOnthName='January';}
else if(cUrntMonth==2){mOnthName='February';}
else if(cUrntMonth==3){mOnthName='March';}
else if(cUrntMonth==4){mOnthName='April';}
else if(cUrntMonth==5){mOnthName='May';}
else if(cUrntMonth==6){mOnthName='June';}
else if(cUrntMonth==7){mOnthName='July';}
else if(cUrntMonth==8){mOnthName='August';}
else if(cUrntMonth==9){mOnthName='September';}
else if(cUrntMonth==10){mOnthName='October';}
else if(cUrntMonth==11){mOnthName='November';}
else if(cUrntMonth==12){mOnthName='December';}
else{}

    AggregateResult[] iKamForcast=[select SUM(IKAM_forecast__c) from Account where Current_month__c=this_year group by CALENDAR_YEAR(Current_month__c)];
    if(iKamForcast.size()>0)
    {
    for (AggregateResult ar : iKamForcast)  {
    iKamForcastSum=(decimal) ar.get('expr0');
   // system.debug('<<<<<<<a value'+iKamForcastSum);
    }
    }
    

    if(iKamForcastSum!=null)
    {
    iKamForcastRatioIn=(Dvol/iKamForcastSum)*100;
    iKamForcastRatio=iKamForcastRatioIn.setScale(0,System.Roundingmode.HALF_UP); 
    }
    
      maxvalue= (dElTarSum>dElComSum&& dElTarSum>cOunForcastSum&& dElTarSum>iKamForcastSum) ? dElTarSum: ((dElComSum>cOunForcastSum && dElComSum>iKamForcastSum) ? dElComSum: ((cOunForcastSum>iKamForcastSum)?cOunForcastSum: iKamForcastSum)) ;  
    }  

 
}

My test class :
@isTest
public class Rfleet_DashboardcurrentYear_Test {    
static testMethod void DashboardcurrentYear(){
test.startTest();
        Id rType =[Select id from RecordType where sObjectType = 'Account' and name='RFLEET-ACC-DCVF-RT'].id;    
        Account acc = new Account(Name='Prabu',Montant__c=5,recordtypeid=rType);
        insert acc;
        system.debug('-------->acc value is ' + acc);
        IKAM_Data__c ikam=new IKAM_Data__c(Annual_deliveries_target__c=45,Annual_global_deliveries_commitment__c=98,IKAM_Year__c=2015,CurrencyIsoCode='INR',Account__c=acc.id);
        insert ikam;
        
        ApexPages.StandardController sc = new ApexPages.standardController(ikam);  
        Rfleet_DashboardcurrentYear accPageCtrl = new Rfleet_DashboardcurrentYear (new ApexPages.StandardController(ikam));     
             
      
        accPageCtrl.Dvol=6.8;   
        accPageCtrl.cOmRatioSum=34.5;
        accPageCtrl.cOunForCastRatio=23.3;
        accPageCtrl.cOunForcastSum=67.8;
        accPageCtrl.cUrntMonth=9.0;
        accPageCtrl.cUrntYear=2015.0;
        accPageCtrl.dElComSum=56.8;
        accPageCtrl.dElTarSum=89;     
        accPageCtrl.iKamForcastRatio=8.7;
        accPageCtrl.iKamForcastSum=56.7;
        accPageCtrl.mOnthName='january';
        accPageCtrl.iKamForcastRatioIn=9.8;
        accPageCtrl.cOunForCastRatioIn=6.7;
        accPageCtrl.tArRatioSum=5.6;
        accPageCtrl.selectedval='hjk';
        accPageCtrl.str='gjg';
        accPageCtrl.s='jh';
        test.stopTest();
}
}

 I have added the screenshot for uncovered lines in my apex class 
User-added image
Best Answer chosen by Andy Boettcher
Andy BoettcherAndy Boettcher
Your AggregateQueries are looking at "Current_Month__c" as query criteria - but you aren't populating that in line 9 of your test class.  Try populating that?

All Answers

Andy BoettcherAndy Boettcher
Your AggregateQueries are looking at "Current_Month__c" as query criteria - but you aren't populating that in line 9 of your test class.  Try populating that?
This was selected as the best answer
James LoghryJames Loghry
Simply put, your queries aren't returning records like they should be.

The first query in question has a filter that asks for current_month__c = this_year, which will likely not work.  You can't compare 2015 with 11 and expect to get back records for instance.

Please adjust either or both your SOQL queries in your controller class and/or the mock data you're creating.  If you fix those items, you will be able to cover the red lines of code.
Umamageshwari PalanisamyUmamageshwari Palanisamy
cuurent_month_c is the formula field .It have the value like 23-11-2015.So I am comparing current_month_c=this_year.It compares the year 2015 with 2015.
 
James LoghryJames Loghry
So you're trying to compare 23-11-2015 with 2015? Nope, still wont work. Try again.
Umamageshwari PalanisamyUmamageshwari Palanisamy
After populating the Curent_month_c i got test coverage  95%.I have populated the formula field in my test class.thank you !!
Andy BoettcherAndy Boettcher
Awesome stuff!  Glad we could help out.