How do I write a proper unit-test for an Apex Trigger?
Writing test classes for Apex triggers is one of the most commonly asked Salesforce questions. Developers often wonder whether the test logic should live “inside” a trigger, in a separate class, or somewhere else. The short answer is that Salesforce requires you to write at least one test method that performs real DML to invoke the trigger, and it must always be written outside the trigger file.
Full Explanation
In Salesforce, every Apex Trigger you deploy to production must be covered by a test class. Even if most of your logic actually lives in a separate handler class, you still need test methods that fire the trigger by performing DML statements.
Salesforce does not allow test code inside trigger files. Test methods must be placed in a separate test class annotated with @isTest.
Modern best practice recommends keeping the trigger itself very small, and delegating the business logic to a separate Apex class. This pattern allows you to write cleaner tests, focus on real logic, and avoid filling your test with unnecessary DML every time you want to test a method.
So you typically test a trigger in two ways. First, by inserting or updating real records so that the trigger executes. Second, by calling your handler class methods directly to achieve better test isolation and more granular testing.
Example Structure
Below is a simple and recommended structure.
The Trigger
trigger AccountTrigger on Account (before insert) {
if (Trigger.isInsert) {
Accounts.onBeforeInsert(Trigger.new);
}
}
This trigger contains almost no logic. It simply delegates the work to an Apex class that handles Account-related behavior.
The Handler Class
public with sharing class Accounts {
public static void onBeforeInsert(List<Account> accounts) {
// Put real business logic here
}
public static void someOtherAccountFunctionality(List<Account> accounts) {
// Additional logic
}
}
All logic sits here. The advantage is that you can test this class directly, with or without DML.
The Test Class
@isTest
private with sharing class AccountsTest {
@isTest
static void testAccountTriggerViaDML() {
Account testAccount = new Account(Name = 'Test Account');
insert testAccount;
testAccount = [
SELECT Id, Name
FROM Account
WHERE Id = :testAccount.Id
];
System.assertEquals('Test Account', testAccount.Name);
}
@isTest
static void testAccountsOnInsertDirectly() {
Account testAccount = new Account(Name = 'Test Account');
Accounts.onBeforeInsert(new List<Account>{ testAccount });
System.assertEquals('Test Account', testAccount.Name);
}
@isTest
static void testSomeOtherAccountFunctionality() {
Account testAccount = new Account(Name = 'Test Account');
Accounts.someOtherAccountFunctionality(new List<Account>{ testAccount });
System.assertEquals('Test Account', testAccount.Name);
}
}
The first test method fires the trigger using normal DML. That is required because Salesforce needs the trigger coverage itself. The next test methods call the handler methods directly, which allows more flexible, isolated unit testing without the need to insert records each time.
Conclusion
To write a proper unit-test for an Apex trigger, you must place your test code in a separate class and execute real DML that causes the trigger to fire. The best practice is to move logic out of the trigger and into an Apex handler class so your test code can focus on small units of logic and not just on the act of firing the trigger.
This combination gives you clean triggers, reusable business logic, better test coverage, easier maintenance, and improved alignment with Test-Driven Development principles.
Real-Time Project-Based Salesforce Training to Kick Start Your Career
Our Salesforce Course is designed to provide a thorough understanding of the Salesforce platform, equipping you with the essential skills to thrive in the CRM industry. The curriculum includes vital modules such as Salesforce Admin, Developer, and AI, combining foundational knowledge with hands-on practice. By engaging in real-world projects and assignments, you’ll develop the expertise to address complex business challenges using Salesforce solutions. Our expert instructors ensure you gain both technical skills and industry insights necessary for success in the Salesforce ecosystem.
Along with technical knowledge, our Salesforce training in Kota offers personalized mentorship, exam preparation, and interview coaching to enhance your career prospects. You’ll have access to comprehensive study materials, live project experience, and dedicated support throughout your learning journey. Upon completing the course, you’ll be well-prepared for certification exams and equipped with the practical skills employers value. Begin your Salesforce career with us and unlock countless career opportunities. Sign up for a Free Demo today!

