I am using Apex DML mocking using stub api and got a reference from multiple blogs, https://github.com/packocz/Pragmatic-Bear-Blog https://github.com/Coding-With-The-Force/Salesforce-Development-Tutorials/tree/main/DML_Mocking https://github.com/gscloudsolutions/GS-Apex-Mocking-Framework

Tried all of them but somehow trigger is not getting called. I am using after insert and before update events in trigger.

So, from test class using mock framework, I am generating the Id and I see the Id is generated in logs. So, expecting trigger should get fired. so, It will cover the code of handler and helper classes.

Please guide me on this if anything i am missing.

  • 2
    If you have mocked out the DML, why would the trigger be called? If you organize your code appropriately, so the trigger simply invokes a trigger handler, you can then unit test the trigger handler without the need to even worry about mocking DML - simply create in-memory-only records with before and/or after state and pass these to your trigger handler methods. NB: Using JSON deserialization techniques (such as those used in Matt Addy's SObjectFabricator allows you to simulate otherwise readonly fields (such as formula fields etc.).
    – Phil W
    Commented May 5, 2023 at 10:46
  • Thanks for your suggestion. I suspected the same but you have cleared my thought. so, bascially I need to call handler methods from mocktest class by passing appropriate details. correct?
    – sumit
    Commented May 5, 2023 at 11:11
  • Yes, that's the right way to unit test this sort of logic.
    – Phil W
    Commented May 5, 2023 at 12:08
  • +1 for using mocking; check out ApexMocks and Amoss for other apex mocking frameworks that have more power than the one you chose
    – cropredy
    Commented May 5, 2023 at 15:48


You must log in to answer this question.

Browse other questions tagged .