[SalesForce] How to cover afterUndelete method when you can’t Undelete

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    }

Best Answer

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.

Related Topic