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
Jancy MaryJancy Mary 

Test class to cover SOQL query on reports that result 0 records.

Hey guys,

Need some help, I have a apex class with soql query on Reports of type (Task & Events) in the UAT, the reports for now returns 0 records as I have some filters as Date = Last 30 Days, I'm not able to cover the query line in test class as the report result 0 count, if the same report result atleat 1 record the query line are covered in the test class. Rest all the code lines are covered except these query lines.

Also a question, while moving the code from sandbox to production through change set will the test class validate against production org data or sandbox.

Thanks in advance,
Jancy Mary
Best Answer chosen by Jancy Mary
Jancy MaryJancy Mary
Hi Yuchen,

Well thanks for your reply on this, you are partially correct, I actually resolved this few days back.

1. Used @isTest(seeAllData='true') on the top class level and just called the methods of controller in the Test Class.
2. As it was my sandbox on which I had my report and controller classes defined, I just created few test records to make the reports have some records under it, this resolved half of the problem. I wrote the below query in my Test Class so that I get some records from the report to pass the Test Class.
List <Report> reportList1 = [SELECT Id, DeveloperName FROM Report where DeveloperName = 'my_Report_API_Name'];
@isTest(seeAllData='true')
public class last30DayCallsController_TestHelper
{    
    public static Integer count=0;  
    public static integer Target; 
           
              
        public static testMethod void table(){ 
        
        test.startTest();
              
        List <Report> reportList1 = [SELECT Id, DeveloperName FROM Report where DeveloperName = 'my_Report_API_Name'];                    
                                
        last30DayCallsController.Data1 data1 = new last30DayCallsController.Data1(600, Count);
        last30DayCallsController.getData1(); //controller method call
        last30DayCallsController.allPlcmt1(); //controller method call
        
	test.stopTest(); 
        }
}


Thanks for all help on this post,

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please create all test data in your test class only. If you will add seeAllData=true then your test class will use your org data. But i will recommend you to create your test data in your test class and use seeAllData=false.

If you will deploy/Validate test class in production and seeAllData=true then it will search data in production if data will not there then your test class will fail that is why i am asking add test data inside test class only.
Jancy MaryJancy Mary
Hi Amit,

Thanks so much for helping me on this, well I have the below apex code & test class,
public class last30DayCallsController{


    public static integer count1=0;
    
    public static List<Data1> getData1() {    
        return last30DayCallsController.allPlcmt1();
    }
        
    public static List<Data1> allPlcmt1() {
                    
        List<Data1> data1 = new List<Data1>();
          
        List <Report> reportList = [SELECT Id, DeveloperName FROM Report where Id = '00O90000004Gm2J'];
        
        String reportId=null;
         reportId = (String)reportList.get(0).get('Id');
        system.debug('SIZE'+reportId);      
        Reports.reportResults results = Reports.ReportManager.runReport(reportId, true);        
        Reports.Dimension dim = results.getGroupingsDown();        
        
        data1.add(new data1(600, count1));
        
        if(dim!=null){
        for(integer i=0; i<dim.getGroupings().size(); i++)
        {
            Reports.GroupingValue groupingVal = dim.getGroupings()[i];        
            String factMapKey = groupingVal.getKey() + '!T';
            Reports.ReportFactWithDetails factDetails =(Reports.ReportFactWithDetails)results.getFactMap().get(factMapKey);
            List<Reports.ReportDetailRow> DetailRow= factDetails.getRows();
                  
         if(DetailRow.size()>0) {
             count1+=DetailRow.size();
             system.debug('$$$$$'+DetailRow);
             data1.add(new data1(600, count1));             
         }                                     
      }
      }        
     return data1;                 
    }
}
 
@isTest
public class last30DayCallsController_TestHelper
{    
    public static Integer count=0;  
    public static integer Target; 
           
        @isTest(SeeAllData='false')
        public static void table(){

List <Report> reportList1 = [SELECT Id,DeveloperName FROM Report where Id='00O90000004Gm2J'];

 last30DayCallsController.Data1 data1 = new last30DayCallsController.Data1(5, 2);
        last30DayCallsController.getData1();
        last30DayCallsController.allPlcmt1();
}

Hilighted line in the test class is the one which is not covered, is it something like I should create & and insert records to a report in test class?
KeshabKeshab
if you havefilter on Created date  and filter on Date = Last 30 Days, then... add below code in ur class

if(Test.isRunningTest()){
        Date = system.today();
}else{
         Date = Last 30 Days
}



 and  create data in your test class. 

Thanks
Keshab
 
Jancy MaryJancy Mary
Omg, @Amit as per your suggestion I changed the (seeAllData='true') to (seeAlllData='false') & the code coverage reduced from 68% to 9%! Any suggestion on this.

Also the lines that are not covered are line 25 to 35 in the apex class, the report '00O90000004Gm2J' at present has no records returned, however when I change the date range on report to show some records, the code coverage is 100%.

@Keshab, thanks for the reply but I din't get how to add your condition in my code.
Jancy MaryJancy Mary
User-added image

Here it is, when I use Date = Last 30 Days it returns no records and the class code from line 25 to 35 is not covered, if I change the Date to Last 60 Days couple of records are returned & the lines get covered.

I have the requirement to for Last 30 Days.

Thanks,  
Jancy MaryJancy Mary
@Amit,

Just to help you, I saw that it is not possible to create Test Report records via apex & so we have to use seeAllData='true', peter has posted about this on other question (https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BJ8KIAW). Not sure what to do, I'm lost.
Amit Chaudhary 8Amit Chaudhary 8
Please try to create some event record in your test class like below

        Account acc = new Account();
        acc.Name='TEst';
        insert acc;
    
        Event E = new Event();
        E.Type = 'Outbound'; // I hope same field is Call Type
        E.Description = '';
        E.OwnerId = ''; // Add CRM Admin here. It may be que then please query that and use
        E.WhatId = acc.id;
        insert E;

 
Jancy MaryJancy Mary
Hi Amit,

Need some help on this still, as you suggested I tried to create test event records, however the methods allPlcmt1() & getData1() still fail as "List <Report> reportList1 = [SELECT Id,DeveloperName FROM Report where Id='00O90000004Gm2J" in my test class return no records. These two methods of apex class code are called in my test class, and these methods actually work on the report result(the report return 0 records for now).

Could you give some clarity for the below points: any reference documents or links would be great.

1. Can we create test records on standard report, in my case I use Report Type- Tasks and Events.
2. Or is it something we should create the same report type with apex code in test class & then create test records on it.
 
YuchenYuchen
I think for you test class, you will have to use "isTest(SeeAllData=true)" for this to work.
Jancy MaryJancy Mary
Hi Yuchen,

Well thanks for your reply on this, you are partially correct, I actually resolved this few days back.

1. Used @isTest(seeAllData='true') on the top class level and just called the methods of controller in the Test Class.
2. As it was my sandbox on which I had my report and controller classes defined, I just created few test records to make the reports have some records under it, this resolved half of the problem. I wrote the below query in my Test Class so that I get some records from the report to pass the Test Class.
List <Report> reportList1 = [SELECT Id, DeveloperName FROM Report where DeveloperName = 'my_Report_API_Name'];
@isTest(seeAllData='true')
public class last30DayCallsController_TestHelper
{    
    public static Integer count=0;  
    public static integer Target; 
           
              
        public static testMethod void table(){ 
        
        test.startTest();
              
        List <Report> reportList1 = [SELECT Id, DeveloperName FROM Report where DeveloperName = 'my_Report_API_Name'];                    
                                
        last30DayCallsController.Data1 data1 = new last30DayCallsController.Data1(600, Count);
        last30DayCallsController.getData1(); //controller method call
        last30DayCallsController.allPlcmt1(); //controller method call
        
	test.stopTest(); 
        }
}


Thanks for all help on this post,
This was selected as the best answer