Site icon Exam4Training

Which scenario is valid for execution by unit tests?

Which scenario is valid for execution by unit tests?
A . Load data from a remote site with a callout.
B . Execute anonymous Apex as a different user,
C . Generate a Visualforce PDF with getContentASPDF
D . Set the created date of a record using a system method.

Answer: C

Explanation:

Unit tests in Salesforce are designed to test Apex code functionality and can execute certain operations within the testing context.

Option C: Generate a Visualforce PDF with getContentAsPDF

In unit tests, you can generate a PDF from a Visualforce page using the

getContentAsPDF() method. This is allowed within test methods and is a valid operation for unit

testing.

Reference: "The getContent and getContentAsPDF PageReference methods can be used in test methods."

― Apex Developer Guide: Testing GET and POST Methods Why Other Options Are Incorrect:

Option A: Load data from a remote site with a callout

Callouts to external services are not allowed in unit tests unless you use a mock callout framework.

Reference: "Test methods can’t make callouts to external systems. To test callouts, use mock callouts by either implementing an interface or using static resources."

― Apex Developer Guide: Testing HTTP Callouts

Option B: Execute anonymous Apex as a different user

Executing anonymous Apex isn’t possible within a test method, and you can’t change the user

context arbitrarily in tests except with System.runAs, which doesn’t execute as a different user for

anonymous code.

Reference: "You can use System.runAs to run code as a different user, but you cannot execute anonymous Apex in this way within a test."

― Apex Developer Guide: Running Apex as Another User

Option D: Set the created date of a record using a system method System fields like CreatedDate are not writeable in Apex tests.

Reference: "You can’t set values for fields that have the Createable attribute set to false, such as system fields like CreatedDate or LastModifiedDate."

― Apex Developer Guide: DML Statements

Latest DEX-450 Dumps Valid Version with 456 Q&As

Latest And Valid Q&A | Instant Download | Once Fail, Full Refund

Exit mobile version