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
Sheslie SenatSheslie Senat 

Cover aggregate result wrapper class in test class

I've got an apex class that contains an aggregateresult. I'm having a hard time getting my test class to cover that section.  Anyone know why? 
Thanks for your help

Apex Class
public class ExpenseReport
{       
    Public String obj {get; set;}
    Public String per {get; set;}
    Public String sta {get; set;}
    Public List <Expenses__c> Oplist {get; set;}
    Public List <Expenses__c> Passlist {get; set;}                
    public Summary[] Summaries { get; set; }




public pageReference ExpenseReport()
    {
        PageReference pg = new PageReference('/apex/Expense_Report');
        pg.setRedirect(false);
        return pg;
    }
    
    Public void getobj() {
        Passlist= new list<Expenses__c> ( [select id, Expense_Status__c, Expense_Incurred_by1__c, Total_Expense_Amount__c, RecordTypeId, Date__c, Description__c, 
                                            GST_Claim__c, HST_Claim__c, QST_Claim__c, Golf_100_Amount__c, Meal_Entertainment_50_Amount__c, Gross_Tip_PST__c, GST__c, 
                                            HST__c, QST__c,Expense_Budget_Owner1__c, Type__c, EOCY__c,Number_of_Attendees__c, Per_Advisor_Share__c, Company_Name__c, 
                                            Expense_Period__c, Name, (select id, Advisor_Name__c from Expense_Contact_Associations__r) from Expenses__c 
                          WHERE CreatedDate = Last_N_Days:330 AND Expense_incurred_by1__c =:obj AND Expense_Period__c = :per 
                          AND (Expense_Status__c = :sta OR (RecordTypeId = :'0121O000001q4sV' AND Expense_Period__c ='Approved') 
                          OR (RecordTypeId = :'0121O000001q4sX'AND Expense_Period__c ='Approved'))
                          order by Type__c, Date__c]);
                          
                                                             
        
        AggregateResult[] results= [select Type__c, SUM(Total_Expense_Amount__c) totexp, SUM(GST_Claim__c) GSTc, SUM(HST_Claim__c) HSTc, 
                                    SUM(QST_Claim__c) QSTc, SUM(Golf_100_Amount__c) Golfamt, SUM(Meal_Entertainment_50_Amount__c)Mealamt, SUM(Gross_Tip_PST__c) Gross, 
                                    SUM(GST__c) GST, SUM(HST__c) HST, SUM(QST__c) QST from Expenses__c 
                          WHERE CreatedDate = Last_N_Days:330 AND Expense_incurred_by1__c =:obj AND Expense_Period__c = :per 
                          AND (Expense_Status__c = :sta OR (RecordTypeId = :'0121O000001q4sV' AND Expense_Period__c ='Approved') 
                          OR (RecordTypeId = :'0121O000001q4sX'AND Expense_Period__c ='Approved'))
                          Group by rollup(Type__c)];
                          
                          Summaries = new List<Summary>();
                            for (AggregateResult ar : results) {Summaries.add(new Summary(ar));
                            }             
           }                                 
     public class Summary {
        public Decimal Totalexp { get; set; }
        public Decimal GSTc { get; set; }
        public Decimal HSTc { get; set; }
        public Decimal QSTc { get; set; }
        public Decimal Golfamt { get; set; }
        public Decimal Mealamt { get; set; }
        public Decimal Gross { get; set; }
        public Decimal GST { get; set; }
        public Decimal HST { get; set; }
        public Decimal QST { get; set; }
        public String Type { get; set; }

        
        
        public Summary(AggregateResult ar) {
            Totalexp = (Decimal) ar.get('totexp'); 
            GSTc = (Decimal) ar.get('GSTc'); 
            HSTc = (Decimal) ar.get('HSTc'); 
            QSTc = (Decimal) ar.get('QSTc'); 
            Golfamt = (Decimal) ar.get('Golfamt');  
            Mealamt= (Decimal) ar.get('Mealamt'); 
            Gross= (Decimal) ar.get('Gross'); 
            GST = (Decimal) ar.get('GST'); 
            HST = (Decimal) ar.get('HST'); 
            QST = (Decimal) ar.get('QST');                        
            if(String.valueof(ar.get('Type__c'))==NULL){
               Type='Total';
               }else{
               Type = (String) ar.get('Type__c'); 
               }                                
        }
    }                            
                                            
     
public void Search() {
    string objquery='select id, Expense_Status__c, Expense_Incurred_by1__c, Total_Expense_Amount__c, RecordTypeId, Date__c, Description__c, GST_Claim__c, HST_Claim__c, QST_Claim__c, Golf_100_Amount__c, Meal_Entertainment_50_Amount__c, Gross_Tip_PST__c, GST__c, HST__c, QST__c, Expense_Budget_Owner1__c, Type__c, Number_of_Attendees__c, Per_Advisor_Share__c, Company_Name__c, Expense_Period__c, Name From Expenses__c where Expense_incurred_by1__c like \'%'+obj+'%\' AND Expense_period__c like\'%'+per+'%\' AND EOCY__c like \'%'+2020+'%\' AND Expense_Status__c like \'%'+sta+'%\' order by Type__c';
    Oplist = Database.query(objquery);
    }

    }



Test Class

@isTest
public class TestExpenseReport {
  
  static testMethod void testExpenseReport() {
  
    String sta = 'Approved,Submitted for Approval';
    String obj = 'Angela Gates';
    String per = 'Feb 15';

ExpenseReport controller = new ExpenseReport();

        Expenses__c e = new Expenses__c(Expense_Status__c ='Approved',
                                        RecordTypeId ='0121O000001q4sV',
                                        Expense_Incurred_by1__c ='Angela Gates',
                                        Expense_Period__c ='Feb 15', 
                                        Description__c ='TestDescrp',
                                        Company_Name__c='TestCompany',
                                        Date__c=System.today(),
                                        GST__c=50,
                                        HST__c=30,
                                        QST__c=20,
                                        PST__c=10, 
                                        Tip_Amount__c=5,
                                        Gross_Amount__c=500,
                                        Number_of_Attendees__c=5);
                                    insert e;
           
     
        Expenses__c e2 = new Expenses__c(Expense_Status__c ='Approved',
                                        RecordTypeId ='0121O000001q4sV',
                                        Expense_Incurred_by1__c ='Angela Gates',
                                        Expense_Period__c ='Jan 15', 
                                        Description__c ='TestDescrp',
                                        Company_Name__c='TestCompany',
                                        Date__c=System.today(),
                                        GST__c=50,
                                        HST__c=30,
                                        QST__c=20,
                                        PST__c=10, 
                                        Tip_Amount__c=5,
                                        Gross_Amount__c=500,
                                        Number_of_Attendees__c=5);
                                    insert e2;
                                       
               
        controller.ExpenseReport();        
        controller.getobj();      
        controller.search();
    
        
 
    }
  
}

@Amit Chaudhary 8
SwethaSwetha (Salesforce Developers) 
HI Sheslie,
To cover the aggregate functions line you must match your if conditions.

The link https://salesforce.stackexchange.com/questions/100382/how-to-write-test-class-for-aggregate-function might help.

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you