Mocking isolates code under test from real dependencies.
Mock callouts with Test.setMock(HttpCalloutMock.class, new MyMock()).
Mock external services via Stub API: apex Test.createStub(MyService.class, new MyStub());
Mock SOQL via dependency injection:
- Define interface for selector.
- Production class queries; test class returns mock data.
Mock Custom Metadata by injecting test instances.
Mock current time by wrapping in TimeService.
Mock Apex callouts in LWC tests via Jest: javascript jest.mock('@salesforce/apex/MyClass.method', () => ({ default: jest.fn() }), { virtual: true });
fflib_ApexMocks — sophisticated open-source mocking library.
Senior insight: mocking enables testing in isolation. Without mocks, tests are slow, flaky, dependent on external state.
