-1
 if(ApexPages.currentPage().getParameters().get('ScheduleFlag') == 'True'){
            schId = ApexPages.currentPage().getParameters().get('schId');
            schReport = [Select Id,Report_Name__c,Selected_Fields__c,Labels__c  from Schedule_Report__c where Id = :schId];
            allFields = schReport.Selected_Fields__c;
            allLabels = New List<string>();
            for(string lab :schReport.Labels__c.split(',')){
              allLabels.add(lab.trim());  
            }
            ScheduleFlag = true;
            directSchedule = true;
            WeeklyFlag = true;
            userList = new list<SelectOption>();
            for(user usr : [SELECT Id,IsActive,Name,Email FROM User where IsActive = true]){
                userList.add(new SelectOption(usr.Email,usr.Name));
            }

            //Next4();
        }
    }
0

2 Answers 2

7

In your unit test, first set the value:

Test.setCurrentPage(Page.myVFPage);
ApexPages.currentPage().getParameters().put('ScheduleFlag','True');

And then call the controller method:

MyController con = new MyController();
con.myMethodThatUsesParameter();

As always, make sure you validate the the output from this method is whatever you're expecting.

7
  • Great minds think alike! You JUST beat me to it... :) Jan 17, 2018 at 20:02
  • @DavidSchach Yeah, what a coincidence. You can have this one if you want it, though. I don't really need the rep.
    – sfdcfox
    Jan 17, 2018 at 20:08
  • Not Working. still not cover that line Jan 17, 2018 at 20:09
  • ApexPages.StandardController sc = new ApexPages.StandardController(ObjReport); ReportGenerationNewCtrl ObjClass = new ReportGenerationNewCtrl(sc); PageReference pageRef = Page.ReportGenerationNew; ApexPages.currentPage().getParameters().put('ScheduleFlag','True'); // ApexPages.currentPage().getParameters().put('schId',string.valueof(ObjReport.id)); Test.setCurrentPage(pageRef); Jan 17, 2018 at 20:11
  • @ArvindTyagi The order of operations is important...
    – sfdcfox
    Jan 17, 2018 at 20:35
1

In your test class, you'll need to set the current page and its parameters:

Test.setCurrentPage(Page.TestPage);
ApexPages.currentPage().getParameters().put('ScheduleFlag', 'True');

Then instantiate the controller as usual.

Not the answer you're looking for? Browse other questions tagged .