2

I have a class that is a triggerhandler for OpportunityLineItem. In my trigger factory it takes all the possible dml statements and passes it to the handler. Before Insert, Before Update, After Delete, After Undelete. This is all well and good as I can write my test class to account for the different statements except for AfterUndelete because apparently OpportunityLineItems as undeletable. I get the error:

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Entity type is not undeletable: []

I can't remove the method altogether from the handler or else I get a compile error that says I must implement the method.

Code example of the methods that I have in the handler:

public override void afterInsert(Map<Id, SObject> newMapGeneric){
    Map<Id, OpportunityLineItem> newMap = (Map<Id, OpportunityLineItem>) newMapGeneric;
    /* AFTER INSERT METHODS START */

    /* AFTER INSERT METHODS END */
}    

public override void afterUpdate(Map<Id, SObject> oldMapGeneric, Map<Id, SObject> newMapGeneric){
    Map<Id, OpportunityLineItem> oldMap = (Map<Id, OpportunityLineItem>) oldMapGeneric;
    Map<Id, OpportunityLineItem> newMap = (Map<Id, OpportunityLineItem>) newMapGeneric;
    /* AFTER UPDATE METHODS START */

    /* AFTER UPDATE METHODS END */
}

public override void afterDelete(Map<Id, SObject> oldMapGeneric){
    Map<Id, OpportunityLineItem> oldMap = (Map<Id, OpportunityLineItem>) oldMapGeneric;
    /* AFTER DELETE METHODS START */

    /* AFTER DELETE METHODS END */
}

public override void afterUndelete(Map<Id, SObject> newMapGeneric){
    // Map<Id, OpportunityLineItem> newMap = (Map<Id, OpportunityLineItem>) newMapGeneric;
    /* AFTER UNDELETE METHODS START */

    /* AFTER UNDELETE METHODS END */
}

When I run my test class the line: public override void afterUndelete(Map newMapGeneric){ does not get covered.

Is there a way to cover it or am I stuck?

EDIT

I created a new method in the test class to cover the undelete:

@isTest static void coverUndelete() {

    // So we can cover the undelete method in the TriggerHandler

    new SL_OpportunityLineItemTriggerHandler().afterUndelete(new Map<Id, SObject>());

}       

When the test runs I get the error:

System.NullPointerException: Attempt to de-reference a null object

Class.SL_TriggerHandlerBase.: line 17, column 1 External entry point Class.NPD_Test_OpportunityLineItemHandler.coverUndelete: line 319, column 1

The TriggerHandlerBase Class:

12     public SL_TriggerHandlerBase(){
13      if (Trigger.old != null){
14          sObjectTypeName = Trigger.old[0].getSObjectType().getDescribe().getName();
15      }
16      else {
17          sObjectTypeName = Trigger.new[0].getSObjectType().getDescribe().getName();
18      }
19    }
2

I would adopt a different handler pattern in that case, personally. If it's forcing you to implement methods you don't even need, it is probably not optimal. I usually opt for a much simpler framework with no interfaces or abstract classes.


But from a pure coverage perspective you can hit the method by simply calling:

new MyHandler().afterUndelete(new Map<Id, SObject>());

In this case, you don't really care what's in the map, since the entire existence of the method is just garbage boilerplate required by your framework.

  • 1
    typo? new MyHandler().afterUndelete(new map<ID, SObject>()); – cropredy Aug 30 '16 at 15:15
  • Indeed. Time for some coffee. – Adrian Larson Aug 30 '16 at 15:16
  • I keep getting Attempt to de-reference a null object error when I run the test. I think because I'm not trying to undelete hence the list is empty. Could I wrap that in a try catch and get the same results? – Dan Wooding Aug 30 '16 at 16:24
  • No, never try that strategy. You'll get "passing" tests that take you hours to figure out, at best. Can you update your OP with what you're trying now? – Adrian Larson Aug 30 '16 at 16:26
  • 1
    Ah, yeah this framework is making your life harder, not easier. Will update my answer shortly, but I cannot recommend strongly enough you build your own framework using components you understand. I believe my framework (outlined in the linked question) is much simpler to get your head around. – Adrian Larson Aug 30 '16 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.