In Salesforce, when we develop an Apex app or any Apex Class, we ensure the code is clean and maintainable to avoid difficulties during testing.
However, testing and debugging can be complex when your code depends on other classes or services.
In this scenario, we utilize dependency injection in Salesforce Apex code. This technique of structuring the code allows Apex classes to receive their dependencies from an external source rather than creating them internally.
In this Salesforce tutorial, we will learn about Dependency Injection in Salesforce and how to use it in Apex classes.
What is Dependency Injection in Salesforce?
In Salesforce, dependency injection is an Apex code pattern used to decouple code components, making them more testable and maintainable.
By injecting dependencies into classes rather than coding them, we can make Salesforce Apex classes more accessible to test, especially in unit tests.
An Apex code that is structured using Dependency Injection involves three main components:
- Client: The class that depends on an external object (dependency).
- Service: The dependency class that the client needs to operate.
- Injector: Code to inject the dependency into the dependent class.
Use Dependency Injection in Salesforce Apex
To understand and implement dependency injection in Salesforce Apex, let’s take an example of a class that sends notifications. Instead of hardcoding the notification method, we’ll use Dependency Injection to inject the specific notification method.
- First, we will create a Public Interface class Notifier.
public interface Notifier {
void send(String message);
}- Now, create a class EmailNotifier that implements the Notifier interface and sends an email notification.
public class EmailNotifier implements Notifier {
public void send(String message) {
System.debug('Email sent: ' + message);
}
}- Now, we’ll create a class using the Notifier class to send notifications. The Notifier instance is passed in through the constructor.
public class NotificationService {
private Notifier notifier;
// Constructor injection
public NotificationService(Notifier notifier) {
this.notifier = notifier;
}
public void notifyUser(String message) {
notifier.send(message);
}
}The class NotificationService uses the Notifier interface but does not depend on a specific implementation. The actual notifier is inserted via the constructor.
- In this step, we will demonstrate how to send notifications using the NotificationService class with the EmailNotifier implementation.
Notifier emailNotifier = new EmailNotifier();
NotificationService notificationService = new NotificationService(emailNotifier);
notificationService.notifyUser('Your order has been shipped!');In the above anonymous apex code, we have created an instance of EmailNotifier, which implements the Notifier interface. This instance will be injected into the NotificationService class.
- Next, we instantiated the NotificationService, passing the EmailNotifier instance through its constructor. With this, the notificationService instance sends a notification message by calling the notifyUser method.

In this code execution, we successfully demonstrated the utilization of the NotificationService by injecting an EmailNotifier instance.
This allows us to send a notification message while maintaining decoupling between the classes.
Write a Unit Test using Dependency Injection
To test the NotificationService class, create a MockNotifier class that logs messages instead of sending real notifications. This mock class helps verify behavior in unit tests.
public class MockNotifier implements Notifier {
public List<String> sentMessages = new List<String>();
public void send(String message) {
sentMessages.add(message);
}
}Follow the below code for the apex test class.
@IsTest
public class NotificationServiceTest {
@IsTest
static void testNotifyUser() {
MockNotifier mockNotifier = new MockNotifier();
NotificationService notificationService = new NotificationService(mockNotifier);
notificationService.notifyUser('Test message');
// Assert that the message was logged in MockNotifier
System.assertEquals(1, mockNotifier.sentMessages.size());
System.assertEquals('Test message', mockNotifier.sentMessages[0]);
}
}If you have executed the code in VS Code, click Run Test, located just below the @isTest annotation.
If you are executing the code in the Salesforce developer console, navigate to Test > New Run, select the Apex test class, and click the Run button.
After executing the test class, if it runs successfully, we have successfully executed the test class.

In this test, MockNotifier implements the Notifier interface and overrides the send method to mock sending a notification.
When we run the test, NotificationService uses the mock version of Notifier, allowing us to verify that it calls send without sending an actual notification.
This makes it easy to verify that NotificationService works as expected without depending on the real behavior of the Notifier.
This way, you can use and implement the Dependency Injection technique in the Apex classes to write more testable and maintainable code.
You may also like to read:
- Access Modifiers in Salesforce Apex Classes
- Create Wrapper Class in Salesforce Apex
- Apex String Methods to Determine Character Types in Salesforce
- Exception Handling in Salesforce Apex
- Apex Trigger Handler and Helper Class in Salesforce

Abhijeet is a skilled Salesforce developer with experience in developing and integrating dashboards, data reports, and Salesforce applications. He is also skilled at optimizing processes and flow automation processes, coding, and executing complex project architecture. Read more about us | LinkedIn Profile.