PD1 Testing and Debugging 5 β Questions and Answers
Question 1: A developer wants their private helper method to be covered by a test class in a different file. What is the correct approach?
- Change the method to global access
- Add @TestVisible to the private method (Correct answer)
- Move the method to an inner class and make the inner class public
- Create a public wrapper method that delegates to the private method
Correct answer: Add @TestVisible to the private method
@TestVisible on a private method allows test classes to call it directly without altering its production-level access modifier.
Question 2: When running Apex tests in a sandbox, a developer notices that some tests fail due to missing custom metadata records. What is the recommended fix?
- Use SeeAllData=true so tests can read custom metadata
- Create the required custom metadata records in the sandbox (Correct answer)
- Use Test.loadData() to insert the custom metadata at runtime
- Mock the metadata using a stub pattern since metadata can't be created in tests
Correct answer: Create the required custom metadata records in the sandbox
Custom Metadata records are accessible from Apex tests without SeeAllData=true and should simply be deployed or created in the sandbox where the tests run.
Question 3: A Scheduled Apex job is submitted during a test with System.schedule(). How does the test verify the job's logic runs correctly?
- Call System.schedule() and then immediately assert the results
- Enqueue the schedulable class separately with System.enqueueJob()
- Wrap the schedule call between Test.startTest() and Test.stopTest() (Correct answer)
- Use a CronTrigger query to force immediate execution
Correct answer: Wrap the schedule call between Test.startTest() and Test.stopTest()
Scheduled jobs enqueued between Test.startTest() and Test.stopTest() execute synchronously when Test.stopTest() is reached, allowing assertions to follow.
Question 4: Which statement best describes code coverage requirements for deploying Apex code to a Salesforce production org?
- Each individual Apex class must have at least 75% code coverage
- The overall org-wide Apex code coverage must be at least 75% (Correct answer)
- Each trigger must have 100% coverage; classes require only 50%
- Code coverage is optional if the code is deployed via Change Sets
Correct answer: The overall org-wide Apex code coverage must be at least 75%
Salesforce requires a minimum of 75% aggregate code coverage across all Apex code in the org, and every trigger must have at least 1% coverage.
Question 5: Which method should a developer use to verify that a specific field on a queried record has been updated after a trigger fires in a test?
- Re-query the record from the database and assert the field value (Correct answer)
- Assert the field directly on the variable passed into the trigger
- Use Test.getEventDeliveryStatus() to confirm the field change
- Call Database.refresh() on the record instance
Correct answer: Re-query the record from the database and assert the field value
After a trigger fires, you must re-query the record with SOQL to retrieve the committed database state and then assert the expected field value.
Question 6: What is the maximum number of debug logs that can be stored per org at one time in Salesforce?
- 10
- 50
- 250 (Correct answer)
- 1000
Correct answer: 250
Salesforce retains up to 250 debug logs per org; when the limit is reached, the oldest logs are automatically deleted to make room for new ones.
Question 7: A developer uses Database.insert(records, false) in a test to allow partial success. How should the test validate which records failed?
- Catch the DmlException thrown for failed records
- Iterate the SaveResult list returned and check isSuccess() on each entry (Correct answer)
- Query the AsyncApexJob table for insert failures
- Check System.lastException() after the insert call
Correct answer: Iterate the SaveResult list returned and check isSuccess() on each entry
Database.insert() with allOrNone=false returns a list of SaveResult objects; calling isSuccess() on each reveals which records succeeded and which failed along with error details.
A developer wants their private helper method to be covered by a test class in a different file.
What is the correct approach?