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
AlexPHPAlexPHP 

Unit Tests for Custom VisualForce Controllers: Setting ApexPages parameters

I cannot seem to find resources about how to set ApexPages parameters for unit tests.

 

I have a controller similar to:

 

public class MyCustomController {

public class visualForceException extends Exception {}

public MyCustomController() {
// if there is no 'id' parameter, throw error and return
if (ApexPages.currentPage().getParameters().get('id') == null) {
ApexPages.addMessages(new visualForceException('Error: No ID specified.'));

return;
}

 

// ... do other stuff here
}

}

 

Here, the constructor will check if there is an 'id' parameter in the current page.

 

My problem is... how do I set the ApexPages 'id' parameter in my TEST class?

 

Message Edited by AlexPHP on 07-08-2009 02:40 PM
Best Answer chosen by Admin (Salesforce Developers) 
AlsoDougAlsoDoug

ApexPages.CurrentPage().getParameters().put('ParamaterName', 'Value');

 

Use that before in the first part of you test method before you call the controller.

All Answers

AlsoDougAlsoDoug

ApexPages.CurrentPage().getParameters().put('ParamaterName', 'Value');

 

Use that before in the first part of you test method before you call the controller.

This was selected as the best answer
AlexPHPAlexPHP

Ah, yes.  I ended up finding that solution in this resource:

 

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

Thanks for the reply, Doug!

Message Edited by AlexPHP on 07-08-2009 02:39 PM