Prove it with tests: Apex practice drills
Apex test classes that fail when the code is wrong: assertions, test data, mocks and callout mocks.
10 drills · Intermediate · Free to read, run with Apex Path ($19/month after a 7-day trial)
- Assert service outcomes (Beginner, 10 min)
Write a test helper that fails the test when a service returns the wrong label. - Create isolated test data (Beginner, 12 min)
Insert your own test Account, then read it back from the database by its Id. - Build a test data factory (Intermediate, 12 min)
Build a test data factory that inserts any number of Accounts with one DML statement. - Test success and exceptions (Intermediate, 15 min)
Write verifiers that fail on a wrong amount, a wrong error message or a missing error. - Check partial DML results (Intermediate, 15 min)
Insert a good and a bad Account with partial DML, and write a verifier that fails on wrong SaveResults. - Reset limits around the act (Intermediate, 15 min)
Use Test.startTest and Test.stopTest to count only the queries the code under test runs. - Prove bulk behavior (Intermediate, 15 min)
Write a list verifier that checks the size and every item, and fails on a wrong 200-item list. - Use a fake collaborator (Intermediate, 18 min)
Replace an outside gateway with a test fake that records calls and can fail once on purpose. - Mock an HTTP callout (Intermediate, 16 min)
Implement HttpCalloutMock and register it with Test.setMock so a client can be tested offline. - Share setup and run as another user (Intermediate, 18 min)
Create a fixture user and Account in @TestSetup, then use System.runAs to insert a Task as that user.