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
Somnath Paul 3Somnath Paul 3 

Test Class for Aggregate Result function

Hi,

Need help in writing Test Class for the below Apex Class which use AggregateResult.
Apex Code:
 
global class AccountProcessor {
    
    @future
    public static void countContacts(List<Id> AccntIds)
    {
        List<Account> list_of_Account = [select Id,sompaul__Number_of_Contacts__c from Account where Id IN: AccntIds];
        system.debug('List of accounts'+list_of_Account);
        List<Account> accountToBeUpdated = new List<Account>();
        Integer Contact_cnt=0;
        //Map<Id,Decimal> = new Map<Id,Decimal>();
        List<AggregateResult> agrRes = [select count(Id),Contact.Account.Id from Contact,Contact.Account where Contact.Account.Id IN : AccntIds group by Contact.Account.Id];
        system.debug('AggregateResult value:'+agrRes);
        
        for(AggregateResult ar: agrRes)
        {
            
            for(Account a:list_of_Account)
            {
                if(a.Id == ar.get('Id'))
                {
                    a.sompaul__Number_of_Contacts__c = (Decimal)ar.get('expr0');
                    accountToBeUpdated.add(a);
                }
            }
            system.debug('Account Data to be updtaed'+accountToBeUpdated);
            
        }
        if(!accountToBeUpdated.isEmpty())
        {
            update accountToBeUpdated;
        }
        
    }
}

Test Class for the above:
@IsTest
private class AccountProcessorTest {
    
    private static testMethod void testAccountProcessor()
    {
        List<Id> accntList = new List<Id>();
        accntList = createAccount();
        Test.startTest();
        AccountProcessor.countContacts(accntList);
        Test.stopTest();
    }
    private static List<Id> createAccount()
    {
        List<Id> accntList = new List<Id>();
        for(Integer i=0;i<10;i++)
        {
            Account a = new Account(Name='Test Account'+i);
            for(Integer j=0;j<5;j++)
            {
                Contact con = new Contact(LastName ='Test Contact'+j,AccountId=a.Id);
            }
            accntList.add(a.Id);
        }
        return accntList;
    }

}

Code Coverage: 50%. Below strike through codes do not fall under test coverage.

global class AccountProcessor {
    
    @future
    public static void countContacts(List<Id> AccntIds)
    {
        List<Account> list_of_Account = [select Id,sompaul__Number_of_Contacts__c from Account where Id IN: AccntIds];
        system.debug('List of accounts'+list_of_Account);
        List<Account> accountToBeUpdated = new List<Account>();
        Integer Contact_cnt=0;
        //Map<Id,Decimal> = new Map<Id,Decimal>();
        List<AggregateResult> agrRes = [select count(Id),Contact.Account.Id from Contact,Contact.Account where Contact.Account.Id IN : AccntIds group by Contact.Account.Id];
        system.debug('AggregateResult value:'+agrRes);
        
        for(AggregateResult ar: agrRes)
        {
            
            for(Account a:list_of_Account)
            {
                if(a.Id == ar.get('Id'))
                {
                    a.sompaul__Number_of_Contacts__c = (Decimal)ar.get('expr0');
                    accountToBeUpdated.add(a);
                }
            }

            system.debug('Account Data to be updtaed'+accountToBeUpdated);
            
        }
        if(!accountToBeUpdated.isEmpty())
        {
            update accountToBeUpdated;
        }
        
    }
}

Can somebody please help here.
Thanks in advance !!
Best Answer chosen by Somnath Paul 3
Preya VaishnaviPreya Vaishnavi
Please insert the accntList in your test class

All Answers

Preya VaishnaviPreya Vaishnavi
Please insert the accntList in your test class
This was selected as the best answer
Preya VaishnaviPreya Vaishnavi
PFB the code snippet for the same.
private static List<Id> createAccount()
    {
        List<account> accntList = new List<account>();
      List<contact> conList = new List<contact>();
        for(Integer i=0;i<10;i++)
        {
            Account a = new Account(Name='Test Account'+i);
         
        }
  if(accntList.size()>0)
{
      insert accntList;
for(integer n=0;n<10;n++)
{
   for(Integer j=0;j<5;j++)
            {
                Contact con = new Contact(LastName ='Test  Contact'+j,AccountId=accntList[n].Id);
    conList .add(con);
            }
}
   }     
 if(conList .size()>0)
      insert conList;
        return accntList;
    }

 
Somnath Paul 3Somnath Paul 3
Thanks Preya for your help. I forget to insert the test records.
It works now.