Plat-Dev-201 practice question 8 of 30
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?
Full set: 204 questionsApex tests run with data isolation and, by default, do not have access to existing data in the Salesforce organization (a concept known as seeAllData=false). The Execute Anonymous window, however, runs in the current user's context and can access all data they have permission to see. If the code logic depends on pre-existing records, it will succeed in Execute Anonymous but fail in the test context unless the test method first creates the necessary data.
A. System.runAs is for testing user permissions and sharing, not for providing data access. The test would still fail if the required data doesn't exist.
B. Calling an @future method requires Test.startTest() and Test.stopTest() but doesn't inherently cause failure if the underlying logic is sound and data is present.
D. A syntax error would prevent the code from being saved or compiled, meaning it could not be run in either the Test Runner or the Execute Anonymous window.
- Salesforce Developers, Apex Developer Guide, "Isolation of Test Data from Organization Data in Unit Tests": "By default, Apex tests don’t have access to pre-existing data in the organization... You must create test data."
- Salesforce Developers, Apex Developer Guide, "Executing Anonymous Apex Code": "The code runs as the current user and can fail to compile if the code violates the user's object- and field-level security settings."
Keep practicing
Take the free Plat-Dev-201 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other Plat-Dev-201 practice questions
- Question 1Which three steps allow a custom Scalable Vector Graphic (SVG) to be included in a Lightn…
- Question 2Which two statements are true about using the @testSetup annotation in an Apex test class…
- Question 3Managers at Universal Containers want to ensure that only decommissioned containers are a…
- Question 4Universal Containers has a Visualforce page that displays a table of every Container__c b…
- Question 5A developer is alerted to an issue with a custom Apex trigger that is causing records to …
- Question 6Which two operations affect the number of times a trigger can fire? Choose 2 answers
