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

How to write test class for a class containing reports and dashboards.

Hi I need to write test class for this please tell me how to write.It has reports and dashboards.
public class Embedded_Analytics_Extn{
    public Id AccPlanId{get; set;}
    public GE_HQ_Account_Plan__c aPlan{get;set;}
    public String DashId{get; set;}
    public String Report0Id{get; set;}
    public String Report0Name{get; set;}
    public String Report1Id{get; set;}
    public String Report1Name{get; set;}
    public String Report2Id{get; set;}
    public String Report2Name{get; set;}
    public String Report3Id{get; set;}
    public String Report3Name{get; set;}
    public String Report4Id{get; set;}
    public String Report4Name{get; set;}
    public String Report5Id{get; set;}
    public String Report5Name{get; set;}
    public String Report6Id{get; set;}
    public String Report6Name{get; set;}
    public String AccPlanName{get; set;}
    Public String DDUNS{get; set;}
    public List<SelectOption> tiers{get;set;}
    public String tier2{get;set;}
    public String tier3{get;set;}
    public String accName{get;set;}
    public Embedded_Analytics_Extn(ApexPages.StandardController controller){
        //Report r0=[select id,name,developerName from Report where developerName='TOTAL_CurrYr_IFRU'];
        Report r1=[select id,name,developerName from Report where developerName='OG_Tier_1_O_G_Region_Current_Year'];;;
        Report r2=[select id,name,developerName from Report where developerName='OG_Forecast_by_PL_Current_Year'];;;
        Report r3=[select id,name,developerName from Report where developerName='OG_Tier_2_Account_Region_Current_Year'];;;
        Report r4=[select id,name,developerName from Report where developerName='OG_SalesMgr_Current_Year_OPEN'];;;
        Report r5=[select id,name,developerName from Report where developerName='OG_Oppty_by_Stage_Current_Year'];;;
        Report r6=[select id,name,developerName from Report where developerName='OG_SalesMgr_Current_Year_Forecast'];;;
        DashId=[select id,developerName from Dashboard where developerName='OG_Account_Plan_Dashboard'].id;
        List<DashboardComponent> comp=[select id,name from DashboardComponent where DashboardId=:DashId];
        Map<String,DashboardComponent> DashCompMap=new Map<String,DashboardComponent>();
        for(DashboardComponent c: comp){
        GE_HQ_Account_Plan__c AccPlan=[select id,name,GE_HQ_DDUNS_fm__c,GE_HQ_DDUNS_Number__c from GE_HQ_Account_Plan__c where id=:aPlan.Id];
        Account acc=[select id,name from Account where id=:AccPlan.GE_HQ_DDUNS_Number__c];;
        tiers=new List<SelectOption>();
        tiers.add(new SelectOption('','None'));
        tiers.add(new SelectOption('Turbo Machinery','Turbo Machinery'));
        tiers.add(new SelectOption('PII Pipeline Solutions','Pipeline Solutions'));
        tiers.add(new SelectOption('Global Services (GS)','Global Services'));
        tiers.add(new SelectOption('Mesurement & Control (M&C)','Mesurement & Control'));
        tiers.add(new SelectOption('Drilling & Surface (D&S)','Drilling & Surface'));
        tiers.add(new SelectOption('Subsea (SS)','Subsea'));
    public PageReference setTiers(){
        return null;
You would go about writing your tests as you normally would, however this is one of the few places were you have to use the seeAllData=true [1] annotation.  I will advise that you use this sparingly and make sure that your tests only rely on the data in those reports and not any other objects in your environment.

If you need additional information about how to write test in general, please let me know.

I do not think you can create a new report via apex for testing.  This is why you are getting that error.  You should enable seeAllData and query for your reports by developer name.  Then use those in your test code.
Jancy MaryJancy Mary
Hi Patrick,

​Could you help me on this, I have similar issue where I followed your suggestion of using the (seeAllData='true') to acces the reports actual data as test records, my report has filter as Last_30_Days & returns no result as of now as it is UAT which was refreshed more than a month ago, the code coverage now is 68%. In case if the report returns records the coverage would be of 100%.

1. I realized the same what you posted about creating a new report via apex for testing.
2. In such scenario if I change filters on the reports as Last_60_Days in order to cover the code more than 75%, will that be a good practice? will I be able to move the code to production org with out any issues? while I move the code below will be the report filter on 

Production: report filter will be Last_30_Days
UAT: report filter will be Last_60_Days

Thanks in advance, 
Jancy Mary
That's going to be the problem when you have seeAllData (and why I never like to recommend using it).  If you have a different names based on which environment, you will need to include logic in your code to select the correct report in your UAT environment or you will need to create the report with the correct name in your UAT.