0

I am using TriggerFramework for implementing a trigger on case. When I started writing tests for this trigger, this catch statement isn't covered by the tests:

trigger CaseMain on Case (before insert, before update, after insert, after update) {
  try {
       TriggerFactory.createAndExecuteHandler(CaseHandler.class);     
  } catch(Exception ex) {
       MilestoneUtils.SendNotificationOnTriggerFailure('CaseMain', ex);
  }
}

The rest of the code is executed, but not the catch batch. I tried to insert a record which would throw a SoqlException, NullPointerException, or a DmlException, but none of these covered the code.

4
  • Does your trigger framework itself catch and handle exceptions?
    – David Reed
    Commented Apr 18, 2018 at 13:20
  • yes in createAndExecuteHandler it is.only get if it didn't get the handler.
    – SFDC_USER
    Commented Apr 18, 2018 at 13:25
  • 1
    If all exceptions are being caught and handled or swallowed in TriggerFactory.createAndExecuteHandler, no exception will ever reach the catch block here, and there's no reason for it to exist.
    – David Reed
    Commented Apr 18, 2018 at 13:27
  • so all exception will go here... public static void createAndExecuteHandler(Type t){ ITrigger handler = gethandler(t); //to check weather handler is exist or not if(handler == null){ throw new TriggerException('handler is not exist' + t.getName()); } execute(handler); }
    – SFDC_USER
    Commented Apr 18, 2018 at 13:29

1 Answer 1

3

One approach that I've taken to solve this problem is to include an @testVisible, private static Boolean forceException.

The code for that would be placed inside of your trigger factory, and would look like this:

public class TriggerFactory{
    @testVisible
    private static forceException = false;

    // Custom exception classes seem to be good practice
    // Doesn't really need to do anything other than extend the standard
    //   exception class
    public TriggerFactoryException extends Exception{}

    public static createAndExecuteHandler(...){
        if(forceException){
            throw new TriggerFactoryException('Forced exception');
        }

        // existing code here

    }
}

Then, in a unit test method, you simply set TriggerFactory.forceException = true;, et voila, easy, guaranteed exception.

3
  • hi @Derek F with this i didn't get my code in catch block.i want my code in Test class for in catch block for casei simply insert case and Case c = new Case(Desk_com_Status__c = 'test',Subject = 'test',Status='Open', SlaStartDate = System.today().addDays(-10)); and then insert c; and TriggerFactory.forceException = true;
    – SFDC_USER
    Commented Apr 18, 2018 at 17:22
  • @sonam I'm afraid that your comment does not make any sense. Including code in comments does not work out very well. This (including clarification and an update to your situation) is something better off as an edit to your original question.
    – Derek F
    Commented Apr 18, 2018 at 17:40
  • Thank you..now my problem get solved.i got where i was wrong.
    – SFDC_USER
    Commented Apr 19, 2018 at 6:13

You must log in to answer this question.

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