PDI practice question 17 of 30

Which scenario is valid for execution by unit tests?

Full set: 201 questions
Answer options
A: Execute anonymous Apex as a different user.
B: Generate a Visualforce PDF with getcontentaAsPDF ().
C: Load data from a remote site with a callout.
D: Set the created date of a record using a system method. Correct answer
Correct Answer:
D. Set the created date of a record using a system method.
Explanation

The Test.setCreatedDate(recordId, createdDate) method is a system utility specifically designed for use within unit tests. It allows a developer to set the otherwise read-only CreatedDate field for a test record. This is crucial for testing time-dependent logic, such as business rules that trigger after a certain number of days from a record's creation. This method can only be used in a test context and provides a valid way to manipulate system data for robust testing scenarios. Why Incorrect Options are Wrong
A. Execute anonymous Apex as a different user. This is incorrect. The System.runAs() method is used within a test to run test code as a specific user, not to execute anonymous Apex blocks.
B. Generate a Visualforce PDF with getContentaAsPDF (). This is incorrect. The PageReference.getContentAsPDF() method is treated as a callout by the system and is not permitted within a test method, causing an exception if called.
C. Load data from a remote site with a callout. This is incorrect. Direct HTTP callouts to external sites are blocked in unit tests to prevent reliance on external systems. Testing callouts requires using a mock response via the HttpCalloutMock interface. ---

Why Incorrect

A. Unit tests execute Apex inside the test method; “execute anonymous” is a tooling feature and cannot be invoked from test code.
B. PageReference.getContentAsPDF() is blocked in test context; the runtime throws a “getContent not allowed in test” exception.
C. Tests can’t make real callouts; they must supply an HttpCalloutMock. A direct remote-site callout therefore fails unless mocked, so the scenario as written isn’t valid.

References

1. Correct Answer (D):

Source: Salesforce Apex Developer Guide

Reference: In the "Testing Apex" section, under "Using the Test Class," the Test.setCreatedDate(recordId, createdDate) method is documented. It states, "Sets CreatedDate for a test-context sObject." This confirms its validity and purpose within unit tests. (See the "Test Class" documentation).

2. Incorrect Option (B):

Source: Salesforce Apex Developer Guide

Reference: In the "Execution Governors and Limits" section, under "Per-Transaction Apex Limits," the documentation lists methods that perform callouts. It explicitly notes that PageReference.getContent() and getContentAsPDF() are treated as callouts and are subject to callout limits and restrictions, including the restriction from being used in test methods.

3. Incorrect Option (C):

Source: Salesforce Apex Developer Guide

Reference: The "Testing HTTP Callouts" section states: "To prevent tests from failing and to increase code coverage, Apex provides a built-in mock framework. You can test callouts by using mock callouts. When you run a test that performs a callout, the callout is not made. Instead, a mock response is returned that you specify in the test." This confirms real callouts are invalid.

4. Incorrect Option (A):

Source: Salesforce Apex Developer Guide

Reference: The section "The runAs Method" explains its purpose: "The runAs method enables you to write test methods that change the user context to an existing user or a new user so that the user’s record sharing is enforced." Its function is to test code under a specific user's permissions, not to execute anonymous Apex.

Scroll to Top