PDI · Question #206
PDI Question #206: Real Exam Question with Answer & Explanation
The correct answer is D: The test method relies on existing data in the sandbox.. An Apex test method failed in the Test Runner but succeeded in Execute Anonymous because test methods run in an isolated environment and do not automatically see existing sandbox data, unlike Execute Anonymous which runs in the current user's context.
Question
While working in a sandbox, an Apex test fails when run in the Test Runner. However, executing the Apex logic in the Execute Anonymous window succeeds with no exceptions or errors. Why did the method fail in the sandbox test framework but succeed in the Developer Console?
Options
- AThe test method has a syntax error In the code.
- BThe test method does not use System. rurAs to execute as a specific user.
- CThe test method Is calling an future method.
- DThe test method relies on existing data in the sandbox.
Explanation
An Apex test method failed in the Test Runner but succeeded in Execute Anonymous because test methods run in an isolated environment and do not automatically see existing sandbox data, unlike Execute Anonymous which runs in the current user's context.
Common mistakes.
- A. If there were a syntax error, the code would fail to compile or execute in both the Test Runner and Execute Anonymous.
- B. While System.runAs is important for testing different user profiles and sharing rules, its absence alone doesn't explain why a test fails due to data dependencies when Execute Anonymous succeeds.
- C. Calling a future method within a test method itself is generally handled differently (Test.stopTest() causes them to execute synchronously), but this doesn't explain a discrepancy based on data access.
Concept tested. Apex test data isolation
Topics
Community Discussion
No community discussion yet for this question.