← Salesforce Platform Developer: Apex and Lightning
07 / 8 · 40 MIN

Useful tests, isolation, and coverage

Demonstrate behavior using controlled inputs and correctly interpret deployment criteria.

Concept and mechanism

A useful test establishes inputs, performs an action, and checks observable outcomes. Coverage percentage indicates exercised lines; it does not prove a calculation, state, or rejection is correct. Creating data in the test or a factory reduces dependence on customers and requests already present in the org. SeeAllData should not be the automatic answer to a test working only in one sandbox. Separating data preparation from the operation between Test.startTest and Test.stopTest supports analyzing the appropriate limit context and observing asynchronous work supported by that pattern. Assertions should check expected effects and conditions that must remain unchanged.

Guided application

Prepare positive, negative, and bulk paths. For an operational transition, test entering the state, editing without transition, and multiple records with different outcomes. The general Apex coverage rule does not remove the need to read requirements for the deployment test level in use. With RunSpecifiedTests, each packaged class and trigger needs at least 75% coverage from selected tests; an 86% aggregate does not compensate for a class at 64%. Meeting that number still does not replace assertions. During release planning, communicate coverage, exercised scenarios, and limitations separately. Local dr.pt checks validate content and small conceptual models; they are not execution of those tests in a Salesforce org.

IN PRACTICE

A calculation has 92% coverage but is never compared with an expected outcome. Adding assertions for a normal case, a boundary, and a rejection establishes whether the rule is correct. Repeating the same test in another browser does not cover those branches.

Common pitfalls

Coverage treated as correctness; real data treated as permanent fixtures; single-record-only tests; aggregate rules incorrectly applied to RunSpecifiedTests.

Related topics: Data model, identity, and metadata · Collections and choosing configuration or code · Queries, DML, and error outcomes

Take this idea with you

Test decisions and outcomes using controlled data and representative volume. Read the coverage rule for the chosen deployment mode.

Create account

Reference: Get Started with Apex Unit Tests · Platform Developer guide Summer 25