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
aressaress 

test class for this soql code

Need TEST CLASS for following

Description: Display sum of all closed Opportunity amount for current year

public class FiscalYear {
public static Map<Integer,Decimal> getSumOfOpportunities() {
Map<Integer,Decimal> opportunityMap = new Map<Integer,Decimal>();
List<AggregateResult> opportunityList = [
SELECT
CALENDAR_YEAR(CloseDate) YEAR,
sum(Amount) SUM
FROM
Opportunity
WHERE
isClosed = true
GROUP BY
CALENDAR_YEAR(CloseDate)
];
for( AggregateResult opportunityRecord : opportunityList ) {
if( opportunityRecord.get('YEAR').equals(System.today().year()) ) {
System.debug('Sum of all closed Opportunity amount for current fiscal year: '
+ opportunityRecord.get('SUM'));
}
opportunityMap.put((Integer)opportunityRecord.get('YEAR'), (Decimal)opportunityRecord.get('SUM'));
}
System.debug(opportunityList);
System.debug(opportunityMap);
return opportunityMap;
}
}
Akshay_DhimanAkshay_Dhiman
Hi Ayisha,
 
Try this test class for your code 
@isTest public class FiscalYear_Test {
    @isTest public static void CreateOpp()
    {
        List<Opportunity> oppList = new List<Opportunity>();
        for(Integer i = 0 ; i <= 50 ; i++)
        {
            Opportunity opp = new  Opportunity();
            opp.StageName = 'Closed Won';
            opp.Name = 'Opp' + i;
            opp.CloseDate = System.today();
            oppList.add(opp);
        }
        if(oppList.size()>0)
        {
            insert oppList;
        }
        Test.startTest();
        FiscalYear.getSumOfOpportunities();
        Test.stopTest();
    }
}
​


Hope it will help you 
Thanks 
Akshay