You need to sign in to do that
Don't have an account?

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:
My test class :
I have added the screenshot for uncovered lines in my apex class

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
All Answers
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.