An Apex Test Class is Apex code marked with @isTest that exercises your production Apex classes. Salesforce requires:
- 75% line coverage for the org's Apex code before deploying to production.
- Every trigger must be touched by at least one test.
- Tests must run successfully — no failures allowed during deploy validation.
Anatomy:
@isTest
private class AccountTriggerTest {
@isTest
static void testInsert() {
Account acc = new Account(Name='Test');
Test.startTest();
insert acc;
Test.stopTest();
System.assertEquals('Test', [SELECT Name FROM Account WHERE Id=:acc.Id].Name);
}
}Key features:
- Test isolation — tests don't see existing org data by default. You build test data with
@testSetupmethods or inline factories. - `Test.startTest()` / `Test.stopTest()` — async ops fired between these run synchronously when
stopTest()is called, letting you assert on their effects. - `System.assertEquals` — assertions that fail the test if values don't match.
Don't write tests just to hit coverage. Write tests that assert business behaviour — coverage is a side effect of good tests.