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
FrescoFresco 

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'];
        //Report0Id=r0.id;
        //Report0Name=r0.name;
        Report r1=[select id,name,developerName from Report where developerName='OG_Tier_1_O_G_Region_Current_Year'];
        Report1Id=r1.id;
        Report1Name=r1.name;
        Report r2=[select id,name,developerName from Report where developerName='OG_Forecast_by_PL_Current_Year'];
        Report2Id=r2.id;
        Report2Name=r2.name;
        Report r3=[select id,name,developerName from Report where developerName='OG_Tier_2_Account_Region_Current_Year'];
        Report3Id=r3.id;
        Report3Name=r3.name;
        Report r4=[select id,name,developerName from Report where developerName='OG_SalesMgr_Current_Year_OPEN'];
        Report4Id=r4.id;
        Report4Name=r4.name;
        Report r5=[select id,name,developerName from Report where developerName='OG_Oppty_by_Stage_Current_Year'];
        Report5Id=r5.id;
        Report5Name=r5.name;
        Report r6=[select id,name,developerName from Report where developerName='OG_SalesMgr_Current_Year_Forecast'];
        Report6Id=r6.id;
        Report6Name=r6.name;
                
        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){
            DashCompMap.put(c.name,c);    
        }
        this.aPlan=(GE_HQ_Account_Plan__c)controller.getRecord();
        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];
        
        AccPlanName=AccPlan.Name;
        DDUNS=AccPlan.GE_HQ_DDUNS_fm__c;
        Account acc=[select id,name from Account where id=:AccPlan.GE_HQ_DDUNS_Number__c];
        this.accName=acc.name;
        /*
        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(){
        this.tier2=aPlan.GE_OG_Tier_2_P_L__c;
        this.tier3=aPlan.GE_OG_Tier_3_P_L__c;
        return null;
    }
    
}
pconpcon
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.

[1] https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_seealldata_using.htm
pconpcon
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.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_analytics_test_reports.htm
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
pconpcon
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.