PDI · Question #22
A developer has a single custom controller class that works with a Visualforce Wizard to support creating and editing multiple subjects. The wizard accepts data from user inputs across multiple…
The correct answer is B. Test.setCurrentPage(pageRef); D. ApexPages.CurrentPage().getParameters().put('input\', 'TestValue'); E. String nextPage -controller.save().getUrl(). To effectively test a Visualforce custom controller in a unit test, developers must simulate the page context, set URL parameters, and verify navigation outcomes.
Question
A developer has a single custom controller class that works with a Visualforce Wizard to support creating and editing multiple subjects. The wizard accepts data from user inputs across multiple Visualforce pages and from a parameter on the initial URL. Which three statements are useful inside the unit test to effectively test the custom controller? Choose 3 answers
Options
- Ainsert pageRef.
- BTest.setCurrentPage(pageRef);
- Cpublic ExtendedController(ApexPages StandardController cntrl) { }
- DApexPages.CurrentPage().getParameters().put('input', 'TestValue');
- EString nextPage -controller.save().getUrl();
How the community answered
(23 responses)- A17% (4)
- B74% (17)
- C9% (2)
Why each option
To effectively test a Visualforce custom controller in a unit test, developers must simulate the page context, set URL parameters, and verify navigation outcomes.
`insert pageRef` is syntactically incorrect; `pageRef` is an `ApexPages.PageReference` object used for page navigation or context, not an SObject that can be inserted into the database.
`Test.setCurrentPage(pageRef)` is essential for simulating the Visualforce page context within a unit test, allowing the custom controller to properly interact with page-related methods and variables.
`public ExtendedController(ApexPages.StandardController cntrl)` is a constructor signature for an extension controller, not a standalone custom controller, and is not a statement used *inside* a unit test to set up testing context.
`ApexPages.CurrentPage().getParameters().put('input', 'TestValue')` allows a unit test to programmatically set URL query string parameters, mimicking how a custom controller would receive input from the initial URL.
`String nextPage = controller.save().getUrl()` is used to capture the `PageReference` returned by controller action methods, enabling the unit test to verify that the controller navigates to the expected page after an action.
Concept tested: Testing Visualforce Custom Controllers
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_testing.htm
Topics
Community Discussion
No community discussion yet for this question.