Which scenario is valid for execution by unit tests?
A. Load data from a remote site with a callout.
B. Set the created date of a record using a system method.
C. Execute anonymous Apex as a different user.
D. Generate a Visualforce PDF with geccontentAsPDF ().
B. Set the created date of a record using a system method.
Explanation:
Why B is Valid?
In unit tests, you can use Test.setCreatedDate(recordId, datetime) to backdate records for testing (e.g., validating time-based logic).
This is a test-specific system method and works only in test context.
Why Others Are Invalid?
A. Load data from a remote site with a callout.
Callouts are blocked in tests unless mocked. Use Test.setMock() to simulate responses.
C. Execute anonymous Apex as a different user.
Anonymous Apex cannot run in tests (use System.runAs() instead to switch users).
D. Generate a Visualforce PDF with getContentAsPDF().
getContentAsPDF() is not supported in tests (it requires a visual render, which tests don’t execute).