You need to sign in to do that
Don't have an account?
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?
You can attach parameters to the page reference as follows:
Ah, that makes sense. Thanks Bob!
PageReference pageRef = Page.YourVFPage;
ControllerClass obj = new ControllerClass();
pageRef.getParameters().put('msgFlag','1');
test.setCurrentPage(pageRef);
obj.methodToTest();