You need to sign in to do that
Don't have an account?
Jancy 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
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
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'];
Thanks for all help on this post,
All Answers
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.
Thanks so much for helping me on this, well I have the below apex code & test class,
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?
if(Test.isRunningTest()){
Date = system.today();
}else{
Date = Last 30 Days
}
and create data in your test class.
Thanks
Keshab
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.
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,
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.
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;
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.
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'];
Thanks for all help on this post,