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 questionsTo 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.
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.
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).
Keep practicing
Take the free Plat-Dev-301 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other Plat-Dev-301 practice questions
- Question 1Which use case can be performed only by using asynchronous Apex?
- Question 2The Account object has a field, Audit_Code__c, that is used to specify what type of audit…
- Question 4There is an Apex controller and a Visualforce page in an org that displays records with a…
- Question 5A developer created and tested a Visualforce page in their developer sandbox, but now rec…
- Question 6Refer to the code below: Lightning Web Component JS file JavaScript import {LightningElem…
- Question 7A developer is developing a reusable Aura component that will reside on an sObject Lightn…
