PD1 Testing and Debugging 2 — Questions and Answers
Question 1: Which System.assert method should you use when you want to verify that two values are equal and display a custom failure message?
- System.assert()
- System.assertEquals() (Correct answer)
- System.assertNotEquals()
- System.debug()
Correct answer: System.assertEquals()
System.assertEquals(expected, actual, message) verifies equality and accepts an optional third parameter for a custom failure message.
Question 2: What is the effect of calling Test.startTest() in an Apex test method?
- It resets all static variables to null
- It opens a new governor limit context for the code under test (Correct answer)
- It begins a mock HTTP callout session
- It pauses all asynchronous jobs already in the queue
Correct answer: It opens a new governor limit context for the code under test
Test.startTest() creates a fresh set of governor limits so the code between startTest/stopTest runs with its own independent limits.
Question 3: A developer needs to test a trigger that sends an outbound HTTP callout. Which class must be implemented to avoid a 'You have uncommitted work pending' error in the test?
- HttpResponse
- Callable
- HttpCalloutMock (Correct answer)
- StubProvider
Correct answer: HttpCalloutMock
Implementing HttpCalloutMock and registering it with Test.setMock() allows Apex tests to simulate HTTP responses without making real callouts.
Question 4: In the Apex debugger, what does the SOQL_EXECUTE_BEGIN log line indicate?
- A SOQL query has finished executing and returned results
- A SOQL query is about to be executed (Correct answer)
- A DML operation triggered by a query has started
- The query optimizer has rewritten the SOQL statement
Correct answer: A SOQL query is about to be executed
SOQL_EXECUTE_BEGIN marks the start of a SOQL query execution in the debug log, while SOQL_EXECUTE_END marks its completion.
Question 5: Which debug log level produces the MOST detailed output and is therefore most useful for tracing complex logic, but also consumes the most storage?
- ERROR
- WARN
- INFO
- FINEST (Correct answer)
Correct answer: FINEST
FINEST is the most verbose log level in Salesforce, capturing granular detail about code execution at the cost of larger log size.
Question 6: What happens if a test method contains a DML statement outside of Test.startTest()/Test.stopTest() and the DML count has already reached the limit?
- Salesforce automatically wraps excess DML in a savepoint
- A LimitException is thrown, failing the test (Correct answer)
- The DML is queued for execution after the test completes
- Salesforce raises the DML limit silently for test context
Correct answer: A LimitException is thrown, failing the test
Exceeding any governor limit, including DML statements, causes a LimitException that halts and fails the test method.
Question 7: A developer sets up test data using @testSetup. Which of the following statements about @testSetup data is TRUE?
- Data is shared across test classes in the same deployment
- Each test method gets its own rollback of the setup data after it runs (Correct answer)
- The setup method runs once per test method invocation
- Test setup data persists in the org after the test class runs
Correct answer: Each test method gets its own rollback of the setup data after it runs
@testSetup data is inserted once per class and each test method operates on a clean copy via automatic rollback after each method completes.
Which System.assert method should you use when you want to verify that two values are equal and display a custom failure message?