Skip to content
StackDecode
IntermediateLesson 22 of 37

Apex testing

You must test to deploy: 75% coverage minimum.

apex
@IsTest
private class PricingServiceTest {
    @TestSetup
    static void setup() {
        insert new Account(Name = 'Test');
    }

    @IsTest
    static void addsTax() {
        Test.startTest();
        Decimal gross = new PricingService().withTax(100);
        Test.stopTest();
        System.assertEquals(120, gross, 'Tax should be 20%');
    }
}
  • Tests create their own data; they do not see org data by default.
  • Deploying to production requires 75% overall coverage and all tests passing, and every trigger needs some coverage.
  • Test positive, negative and bulk (200 records) cases. Assert results; coverage alone proves nothing.
  • Use Test.setMock for callouts, System.runAs to test as another user.