I have written some apex triggers along with some classes. The triggers are quite small because all the real work happens in the classes.
trigger MyAccountTrigger on Account (after insert, after update, before delete) {
AccountHandler accountSync = new AccountHandler();
if (Trigger.isInsert) {
handler.processInserts(Trigger.new);
}
if (Trigger.isUpdate) {
handler.processUpdates(Trigger.new);
}
if (Trigger.isDelete) {
handler.processDeletes(Trigger.old);
}
}
I need to write unit tests for this code.
Is it better to write true unit tests that tests only test the AccountHandler
class and another that only tests the trigger while mocking the dependencies? Can you even do dependency injection inside a trigger?
Or is it better to write a test that simply creates/updates/deletes Account
objects, causing the trigger to be naturally executed? That would exercise both the trigger and AccountHandler
class but it's really more of an integration test rather than a unit test.
What is the standard approach for this?