Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
Salesforce Developer
easy

What is an Apex Test Class and why is it required?

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:

apex @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 @testSetup methods 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.

Why this answer works

Foundational. The "75% coverage required" is well-known; mentioning Test.startTest/stopTest pattern and the "don't test for coverage" insight signals real practice.

Follow-ups to expect

Related dictionary terms