Plat-Dev-301 practice question 3 of 10

How should a developer verify that a specific Account record is being tested in a test class for a Visualforce controller?

Full set: 160 questions
Answer options
A: Insert the Account into Salesforce, then use System.setParentRecordId().get() to set the Account ID.
B: Insert the Account in the test class, instantiate the page reference, then use ApexPages.currentPage().getParameters().put() to set the Account ID. Correct answer
C: Instantiate the page reference, insert the Account, then use seeAllData=true to view the Account.
D: Instantiate the page reference, insert the Account, then use System.setParentRecordId().get() to set the Account ID.
Correct Answer:
B. Insert the Account in the test class, instantiate the page reference, then use ApexPages.currentPage().getParameters().put() to set the Account ID.
Explanation

To test a Visualforce controller that depends on a record ID from the URL, the test method must simulate this context. The correct procedure is to first create and insert the necessary test data (the Account). Then, instantiate a PageReference and use ApexPages.currentPage().getParameters().put('id', testAccount.Id) to add the record's ID as a URL parameter. Finally, instantiate the controller. This setup ensures the controller operates on the specific record created within the test, isolating the test from org data and making it predictable.

Why Incorrect

A. System.setParentRecordId() is not a valid Apex method. The correct approach involves setting URL parameters on a PageReference.
C. Using seeAllData=true is a poor practice that makes tests dependent on org data and non-repeatable. It does not set the specific record context for the controller.
D. System.setParentRecordId() is not a valid Apex method. This option incorrectly identifies the method for setting the controller's context.

References

1. Visualforce Developer Guide, "Testing Custom Controllers and Controller Extensions," Section: "Setting Page Parameters in a Test". It provides an example: PageReference pageRef = Page.myVFPage; Test.setCurrentPage(pageRef); ApexPages.currentPage().getParameters().put('id', '001D000000IRt53');

2. Apex Developer Guide, "Testing Best Practices," Section: "Isolating Test Data from Organization Data in Tests". This section emphasizes creating test data and avoiding (SeeAllData=true).

Scroll to Top