Improve Apex testability with dependency injection and mocking strategies for Salesforce. Practical patterns, Test.setMock and Test.createStub tips for reliable, fast tests.
## Why testable design matters in Apex
Good unit tests are fast, reliable, and isolated. In Salesforce projects those traits are harder to achieve when business logic is tightly coupled to platform services (HTTP callouts, static methods, or global singletons). Designing for testability—primarily via dependency injection (DI) and mocking—reduces flakiness, shortens feedback loops, and makes refactors safer.
## Core patterns for testable Apex
### Prefer interface-based design
Extract behavior behind interfaces. Instead of calling a concrete helper or a static utility, define an interface such as PaymentService with methods like charge(Order). Implement the real service for production and a lightweight fake for tests. Interfaces enable you to swap implementations without changing calling code.
### Use constructor or setter injection
Inject dependencies through constructors or setters rather than instantiating them inside methods. Example: MyController(Service s) or setService(Service s). This makes it trivial to supply a mocked implementation in tests and keeps production wiring minimal.
### Avoid static-heavy designs
Static methods are convenient but hard to replace in tests. If you must use statics, wrap them behind an interface implementation so tests can supply a mock.
## Practical Apex mocking techniques
### Test.setMock and platform mocks
For platform abstractions like HTTP callouts, use Test.setMock(HttpCalloutMock, mockInstance). This is straightforward and supported out of the box for callouts, web services (WebServiceMock), Continuations, and some other platform features.
### Test.createStub and interface stubbing
Test.createStub lets you create dynamic stubs for Apex interfaces. For simple behaviors you can implement a stub that returns canned responses, avoiding handwritten mocks:
- Use Test.createStub(MyInterface.class, new MyStub());
- Call code that depends on MyInterface and exercise different responses via the stub.
### Use lightweight fakes for business logic
For internal services, write small in-memory fakes instead of full mocking frameworks. Fakes are easier to reason about and faster to write. For example, an InMemoryQueueableExecutor that simulates enqueuing without asynchronous execution.
### Consider a DI factory or service locator for wiring
A small factory that returns implementations based on context (Test.isRunningTest() or a configuration flag) centralizes wiring. Prefer explicit injection in new code; a factory is useful for migrating legacy code.
## Practical checklist before writing a test
- Can I swap the dependency via a constructor or setter? If not, refactor.
- Is there a platform mock available (HTTP, web service, continuation)? Use Test.setMock.
- Can I use Test.createStub or a simple fake to simulate behavior? Prefer a fake for simple logic.
- Avoid Test.isRunningTest() checks that change behavior—it hides design problems.
## Conclusion
Designing for testability with dependency injection and mocks pays off in reliability and speed. Start by extracting interfaces, injecting dependencies, and using platform mocks or Test.createStub for external behavior. If you want to generate robust Apex tests quickly, try Test Class Generator to automate mocks and stubs tailored to your codebase.