Could you please let me know if we create Test classes for Triggers only? or do we create a test class separately for Handlers too?

1 Answer 1


Generally speaking, you'll want to have one test class for every artifact.

Have a trigger? Write a test class for it

Have an Apex class? Write a test class for it

Yes, a single test class can give you coverage for multiple artifacts, but smaller, more focused tests are generally easier to write. Ideally, you'd have "unit" tests which stress a single piece of functionality for a single at a time as well as "integration" tests which aim to ensure that the individual units work as expected when they're chained together (i.e. a triggger calls class A, then feeds the output into class B, which then internally calls class C).

  • But my seniors are saying for If the class is getting called from Trigger then we should have a test class only for Trigger not for the called Apex class from Trigger. Are they correct?
    – user91530
    Commented Mar 12, 2021 at 12:22
  • @user91530 No, they are not correct (or at the very least they are not giving you the whole story). Writing a test for a trigger here is closer to an integration test. It'll give you coverage for the other classes it ends up executing, but it is harder to set up such a test (because you likely need to insert a bunch of test records prior to executing the trigger, and must perform DML to cause the trigger to execute). As I said, best practice is to have both unit tests (which stress a single part of a single class) and integration tests (which stresses how things work together).
    – Derek F
    Commented Mar 12, 2021 at 12:54
  • @user91530 and because I suspect this is an issue too, the main point of unit tests is not to gain code coverage. Unit tests are useful when you use assertions to verify that the code being tested is producing the output you expect. You should also be testing "negative" cases (where the input is not what you expect it to be, e.g. your method takes an Integer argument but you get passed a null). You can have 100% coverage of a method called do2Plus2(), but if it returns 5 (or anything other than 4), it still has a problem that needs to be fixed.
    – Derek F
    Commented Mar 12, 2021 at 13:23
  • Thanks Derek for taking time to explain it so patiently. I really appreciate your time and efforts.
    – user91530
    Commented Mar 12, 2021 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .