function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nagendra Singh 42Nagendra Singh 42 

Apex unit test for handler methods?

For writing unit test for trigger handler, is it necessay to call the methods in trigger handler by giving an insert, update or delete statement?
Or Can I call the trigger handler methods directly and pass the required argument through the method parameters.

The second approach seems to be the correct way of unit testing the handlers and other classes except the trigger. This will ensure that you have a set of data before passing it to the method, rather than depending upon the trigger to execute the handler methods.

Please suggest which one is better?
RoninCWalkerRoninCWalker
For me, I test both way.
  1. Simulating CRUD within testing scope Test.startTest() with asserting the result.
    • This will help to simulate other related trigger,validation rule,workflow, etc  in the System.
    • It will also tell you if you hit any governance limit.
  2. Unit test individual public method in the trigger handler.
In otherwords, I cover more by testing both and that is the purpose of testing :)