PDI · Question #43
PDI Question #43: Real Exam Question with Answer & Explanation
The correct answer is B: The test method relies on existing data in the sandbox.. The Apex test method failed in the sandbox but passed in the Developer Console because it relied on existing data that was unavailable in the isolated test execution context.
Question
A developer observes that an Apex test method fails in the Sandbox. To identify the issue, the developer copies the code inside the test method and executes it via the Execute Anonymous tool in the Developer Console. The code then executes with no exceptions or errors. Why did the test method fail in the sandbox and pass in the Developer Console?
Options
- AThe test method has a syntax error in the code.
- BThe test method relies on existing data in the sandbox.
- CThe test method is calling an @future method.
- DThe test method does not use System.runAs to execute as a specific user.
Explanation
The Apex test method failed in the sandbox but passed in the Developer Console because it relied on existing data that was unavailable in the isolated test execution context.
Common mistakes.
- A. A syntax error would prevent the code from compiling or executing in both the test method and Execute Anonymous, not just failing in the test method at runtime.
- C. Calling an
@futuremethod in a test method typically queues it for asynchronous execution and doesn't cause the test to fail if the method logic is sound; it might requireTest.stopTest()to ensure completion within the test context. - D. While
System.runAsis important for testing user permissions and access, its absence would typically lead to failures related to insufficient permissions or incorrect user context, not a data-dependent scenario where it passes in Execute Anonymous.
Concept tested. Apex Test Data Isolation
Topics
Community Discussion
No community discussion yet for this question.