function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SidViciousSidVicious 

Code Coverage for Test Class for Undelete in Apex

Hi, everyone. I've hit yet another issue with code coverage for Test classes. This time it's about the Undelete operation.

Trigger
trigger InvoiceLineTrigger on InvoiceLine__c (before insert, before update, before delete, after undelete) {
    new InvoiceLineTriggerHandler().run();
}

Test class
@isTest
    private static void undeleteInvoiceLines(){

        initializeData(); // populates all needed records

        System.assertNotEquals(0, invoiceLineList.size());

        insert invoiceLineList;
        
        delete invoiceLineList;

        Test.startTest();

        	try
			{
                undelete invoiceLineList;
			}Catch(Exception e)
            {
                String errormessage = e.getMessage();
                system.debug(errormessage);
            }

        Test.stopTest();
    }

Apex class
public override void afterUndelete() {

        for(InvoiceLine__c invLine : (List<InvoiceLine__c>) Trigger.old){
            if(invLine.InvoiceId__c != null){
                invLineIdSet.add(invLine.InvoiceId__c);
            }
        }
    }

I'm not able to get code coverage for below line in my Apex class (shown on the code snippet above):
if(invLine.InvoiceId__c != null){
            invLineIdSet.add(invLine.InvoiceId__c);
        }

My assertion above passed, so I know I have the list of Invoice Line records - yet I'm not able to get code coverage for this Undelete operation. 

I'd appreciate any ideas/suggestions on how to resolve this.

Thanks!

- Sid
 
Best Answer chosen by SidVicious
Abhishek BansalAbhishek Bansal
Hi Sid,

There is nothing old when you undelete any records. It is just similar to the insert case. Please change the apex class as below:
public override void afterUndelete() {

        for(InvoiceLine__c invLine : (List<InvoiceLine__c>) Trigger.new){
            if(invLine.InvoiceId__c != null){
                invLineIdSet.add(invLine.InvoiceId__c);
            }
        }
    }
Let me know if this works for you.

Thanks,
Abhishek Bansal.