Section 5 · Testing, Debugging, and Deployment · 20% of exam
5.1 Apex Tests, Test Data, and Reliable Assertions
Exam objective
Write and execute tests for Apex triggers, controllers, classes, and asynchronous behavior using appropriate sources of test data.
Test behavior, not line execution
Apex unit tests should prove that code produces the expected outcome. A test can reach every line and still fail to check whether the code is correct. Arrange test data, invoke the method or perform the DML event that exercises the trigger, and assert observable results. Assertions should explain the expected business outcome: a field value changed, a related record was created, an invalid request was rejected, or no unauthorized record was returned.
Test both normal and negative paths. Include boundary conditions such as a missing optional value, an empty collection, a maximum expected input, and a record that should not meet entry criteria. For trigger code, test one record and a collection. Bulk testing catches queries or DML performed inside loops and logic that handles only the first record. If a trigger updates parents, create several children that refer to repeated and distinct parents so the test verifies grouping and aggregation behavior.
Build test data deliberately
Apex tests generally do not see existing org records by default. Create the records your scenario needs inside the test, often with a test-data factory for shared setup. Keep data focused: unrelated accounts, users, and fields make a test harder to understand. Use @TestSetup when many methods require the same base data, while ensuring one test cannot rely on changes made by another. Tests should not depend on manually maintained org records that may disappear or change. Some data is provided by the platform, such as certain metadata and setup records. Understand the difference between setup objects and business data if a test uses both. Inserting setup and non-setup objects in a single transaction may raise mixed-DML errors; Salesforce provides supported patterns such as System.runAs and asynchronous boundaries for applicable test cases. Do not add complicated workarounds unless the scenario requires them.
SeeAllData=true exposes actual org records and is usually a poor default because the test can pass in one org and fail in another. Use it only when a specific data type or platform behavior requires it and the dependency is understood. For external services, use HttpCalloutMock or another documented mock instead of reaching a live endpoint.
Isolate the code under test
Test.startTest() and Test.stopTest() create a fresh governor-limit context for the operation between them and cause supported asynchronous work to complete in the test context. Prepare test data before startTest(); invoke the relevant work inside the boundary; then assert results after stopTest(). This arrangement makes it clear which operation the test measures. It does not erase all work performed earlier or excuse excessive setup.
Test Queueable, future, scheduled, and batch behavior through the supported test mechanisms. For Batch Apex, verify the records and status produced by the execute work, not only that a job was submitted. For callouts, configure a mock response and assert how the class handles success, error, and malformed responses. Use System.runAs to exercise user-context behavior, while remembering that tests and API-version security defaults should be interpreted according to current documentation.
A strong test method name states the condition and expected result, such as updatesParentForMultipleCases or rejectsApprovalWhenReasonMissing. Clear names turn a test failure into useful diagnostic information.
Extra concept: coverage is a deployment gate, not quality evidence
Salesforce requires Apex coverage to deploy code, but a high percentage only means lines executed. It does not prove bulk behavior, correct calculations, permission enforcement, or error handling. Add assertions for business outcomes and include a regression test whenever a production defect is fixed.
Key takeaways
- Arrange controlled test data, act on the code path, and assert business outcomes.
- Cover negative, boundary, bulk, asynchronous, and permission-sensitive behavior where relevant.
- Use mocks for callouts and treat coverage as a deployment requirement rather than proof of correctness. Official references:
- Salesforce Certified Platform Developer Exam Guide: https://help.salesforce.com/s/articleView?id=005298965&language=en_US&type=1
- Apex testing overview: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/apex-intro-writing-tests.html
- Apex test data: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/apex-testing-data.html
- Apex testing best practices: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/apex-testing-best-practices.html
- startTest and stopTest: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/apex-testing-tools-start-stop-test.html
- Callout testing: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/apex-callouts-wsdl2apex-testing-dml.html
