nerdexam
Salesforce

PDI · Question #60

Which scenario is valid for execution by unit tests?

The correct answer is B. Set the created date of a record using a system method.. Unit tests can set system fields like CreatedDate on records using specific system methods provided for testing purposes.

Submitted by certguy· Apr 18, 2026Testing, Debugging, and Deployment

Question

Which scenario is valid for execution by unit tests?

Options

  • ALoad data from a remote site with a callout.
  • BSet the created date of a record using a system method.
  • CExecute anonymous Apex as a different user.
  • DGenerate a Visualforce PDF with geccontentAsPDF ().

How the community answered

(31 responses)
  • A
    3% (1)
  • B
    94% (29)
  • D
    3% (1)

Why each option

Unit tests can set system fields like CreatedDate on records using specific system methods provided for testing purposes.

ALoad data from a remote site with a callout.

Callouts to external services are strictly prohibited in unit tests unless they are explicitly mocked using Test.setMock(), making direct loading of data with a callout invalid.

BSet the created date of a record using a system method.Correct

Salesforce unit tests allow setting system fields like CreatedDate, LastModifiedDate, and Id on test records. This is typically done using methods like Test.setCreatedDate(recordId, dateTime) or by assigning values to test records before insertion, which is crucial for testing time-sensitive logic or historical data.

CExecute anonymous Apex as a different user.

Anonymous Apex is executed directly from the Developer Console or API and is not a valid component of a unit test method; System.runAs() can change the user context within a test method, but it's not "executing anonymous Apex."

DGenerate a Visualforce PDF with geccontentAsPDF ().

Generating Visualforce PDFs using getContentAsPDF() is generally not allowed within unit test methods because it can consume excessive resources and cause governor limit issues.

Concept tested: Unit test limitations and capabilities

Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_system_fields.htm

Topics

#Unit Testing#Apex Testing#Test Data Setup#System.Test methods

Community Discussion

No community discussion yet for this question.

Full PDI Practice