SP Testing and Quality Assurance 3 — Questions and Answers
Question 1: What is the purpose of using `System.runAs(user)` in an Apex test?
- To test code behavior under a specific user's permissions and profile (Correct answer)
- To bypass all sharing rules during the test method
- To run the test as a System Administrator regardless of the current user
- To test code that requires platform events to fire
Correct answer: To test code behavior under a specific user's permissions and profile
`System.runAs()` executes the enclosed code block as the specified user, allowing tests to verify permission-based and sharing-based behavior.
Question 2: Which scratch org feature enables developers to define source tracking and test against a clean Salesforce environment?
- Scratch orgs created with `sf org create scratch` provide isolated, trackable dev/test environments (Correct answer)
- Full sandboxes with 'Source Tracking' enabled in Setup
- Partial copy sandboxes refreshed daily for clean state
- Developer Pro sandboxes with metadata tracking turned on
Correct answer: Scratch orgs created with `sf org create scratch` provide isolated, trackable dev/test environments
Scratch orgs are purpose-built, disposable environments created from source definitions that support source tracking out of the box.
Question 3: How can a developer verify that a Visualforce page renders correctly without deploying to a full sandbox?
- Use the Visualforce Preview in Developer Console or a Developer Edition org with test data (Correct answer)
- Execute an Apex test class that asserts page output via PageReference
- Use the Lightning App Builder preview mode to render Visualforce components
- Run the page URL through Workbench's REST Explorer
Correct answer: Use the Visualforce Preview in Developer Console or a Developer Edition org with test data
Developer Console's Visualforce preview and Developer Edition orgs allow developers to render and inspect pages interactively before deployment.
Question 4: What does it mean when an Apex test class has 'SeeAllData=false' (the default)?
- The test runs in an isolated context and cannot access existing org data, only data created in the test (Correct answer)
- The test cannot create new records and must use pre-existing data
- The test is excluded from code coverage calculations
- The test can only query standard objects, not custom objects
Correct answer: The test runs in an isolated context and cannot access existing org data, only data created in the test
The default `SeeAllData=false` isolates tests from production/sandbox data, requiring tests to create their own test data for reliable, portable tests.
Question 5: A Salesforce developer is writing tests for a batch Apex class. What is the recommended maximum batch size to use in tests?
- 200 records or fewer to stay within governor limits during testing (Correct answer)
- 2,000 records to simulate full production loads
- 1 record to test the minimum execution path
- 5,000 records to test against the maximum Batch Apex limit
Correct answer: 200 records or fewer to stay within governor limits during testing
Tests typically use 200 records or fewer for batch classes; Salesforce recommends keeping batch size small in tests to avoid hitting DML and SOQL limits.
Question 6: Which approach helps prevent flaky tests caused by record ordering in SOQL queries within test methods?
- Always include an ORDER BY clause in SOQL queries used within test assertions (Correct answer)
- Use LIMIT 1 on all queries to return only one record
- Enable 'Deterministic Queries' in Salesforce Setup > Apex Settings
- Use Test.startTest() before all SOQL queries in assertions
Correct answer: Always include an ORDER BY clause in SOQL queries used within test assertions
Without ORDER BY, SOQL result ordering is not guaranteed, so always specify ORDER BY in test queries that depend on record position.
Question 7: What is the role of a stub object in Apex unit testing?
- It replaces a real dependency with a controlled fake that returns predetermined values without executing real logic (Correct answer)
- It inserts test records into the database before the test method runs
- It captures all DML operations performed during a test for later assertion
- It prevents governor limits from being enforced during the test block
Correct answer: It replaces a real dependency with a controlled fake that returns predetermined values without executing real logic
Stubs (used via StubProvider interface) simulate dependencies like services or selectors, allowing true unit isolation without real callouts or DML.
What is the purpose of using `System.runAs(user)` in an Apex test?