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.
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)- A3% (1)
- B94% (29)
- D3% (1)
Why each option
Unit tests can set system fields like CreatedDate on records using specific system methods provided for testing purposes.
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.
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.
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."
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
Community Discussion
No community discussion yet for this question.