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
ildiavolorossoildiavolorosso 

Unit Testing a Controler that uses Raw URL Parameters

I'm hoping people can think me through a bit of a pickle I've gotten myself into. I have a custom controller for a relatively non-standard Visualforce page that simply iterates through HTTP POST or GET parameters and looks for specific patterns. In a nutshell, the action in question looks like this:

      public PageReference reconcile() {
            // get request parameters
            Map<String, String> parameters = ApexPages.currentPage().getParameters();
            for (String pName: parameters.keySet()) {
                    String pVal = parameters.get(pName);
                    System.debug('PARAM NAME: ' + pName + ' PARAM VAL:' + pVal);
                    
                    // parse the Assessment ID out of the parameter name
                    if (pName.contains('reconcile')) {
                        String assesmentId = pName.replace('reconcile', '');
                        System.debug('RECONCILING ASSESSMENT ID: ' + assesmentId);
                        
// ...work gets done here
                        }                                    
                    }
            }
}

 

So rather than storing state in the controller, I work iteratively. But now I'm having trouble achieving code coverage. My unit test looks something like this:

 

 

       PageRef pageRef = Page.ReconcilePayments; // this is the page my controller is attached to
       Test.setCurrentPage(pageRef);
       ReconcilePaymentController recController = new ReconcilePaymentController();
       System.assert(recController != null);

Now, I can call the reconcile() action directly, but in order to achieve test coverage (and, more importantly,  a good meaningful test) I need to include the parameters and I don't see a way to do that in the pageRef methods...pageRef seems to represent the result of invoking a page, and does not include the request.

 

How would you approach getting this unit test done?

 

 

 

bob_buzzardbob_buzzard

You can attach parameters to the page reference as follows:

 

PageRef pageRef=Page.ReconcilePayments;
Test.setCurrentPage(pageRef); ApexPages.currentPage().getParameters().put('reconcile', 'id1');

 

 

 

desmoquattrodesmoquattro

Ah, that makes sense. Thanks Bob!

Shubham SonarShubham Sonar
hey bob, your answer helped me.

PageReference pageRef = Page.YourVFPage;       
ControllerClass obj = new ControllerClass();
pageRef.getParameters().put('msgFlag','1');
test.setCurrentPage(pageRef);
obj.methodToTest();