nerdexam
Salesforce

PDI · Question #206

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…

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.

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

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.

How the community answered

(27 responses)
  • A
    7% (2)
  • B
    4% (1)
  • C
    4% (1)
  • D
    85% (23)

Why each option

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.

AThe test method has a syntax error In the code.

If there were a syntax error, the code would fail to compile or execute in both the Test Runner and Execute Anonymous.

BThe test method does not use System. rurAs to execute as a specific user.

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.

CThe test method Is calling an future method.

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.

DThe test method relies on existing data in the sandbox.Correct

Apex tests, by default, do not have access to existing organizational data; they must create their own test data (using Test.startTest() and Test.stopTest() with DML operations) to ensure repeatable and isolated tests. When the logic runs in Execute Anonymous, it operates in the context of the logged-in user, which does have access to all existing sandbox data, allowing the method to succeed if it implicitly depends on that data.

Concept tested: Apex test data isolation

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

Topics

#Apex Testing#Test Data Isolation#Execute Anonymous#Sandbox Data

Community Discussion

No community discussion yet for this question.

Full PDI Practice