Stack Exchange
Apex Class Not Receiving Code Coverage
My test class is passing, however the method in my Handler is not receiving any code coverage. How can I fix this?
Handler Class
 /*
     When a Contact is deleted from the Account, 
     update the Number_Of_Contacts field on the Account
*/
    public static void onAfterDelete(List<Contact> oldData){
        Set<Id> accountIds = new Set<Id>();
        for(Contact con : oldData){
            accountIds.add(con.AccountId);
        }
        List<Account> accountsWithContacts = [SELECT Id, Number_Of_Contacts__c,
                                             (SELECT Id FROM Contacts)
                                             FROM Account WHERE Id IN: accountIds];
        for(Account a : accountsWithContacts){
            a.Number_of_Contacts__c = a.Contacts.size();
        }
        update accountsWithContacts;
    }

Test Class
private static Integer testSize = 5;
    
    @testSetup
    static void testDataCreation(){
        
        List<Account> accounts = new List<Account>();
        for(Integer i = 0; i < testSize; i++){
            accounts.add(new Account(Name='Account Name' +i));
        }
        insert accounts;
        
        List<Contact> contacts = new List<Contact>();
        for (Integer j = 0; j < testSize; j++){
            Account acct = accounts[j];
            contacts.add(new Contact(LastName='ContactLname' +j,
                                    AccountId=acct.Id));
        }
        insert contacts;
@isTest
    static void updateAccountAfterDelete(){
        List<Contact> createdContacts = [SELECT Id, AccountId, Name FROM Contact];
        Contact con = new Contact();
                con.LastName = 'deleteContact';
        insert con;
        delete con;  

Trigger
trigger ContactTrigger on Contact (before insert, after insert, after delete) {
    
    If(Trigger.isInsert && Trigger.isAfter){
        ContactTriggerHandler.onAfterInsert(Trigger.new);
    }
    If(Trigger.isAfter && Trigger.isDelete){
        ContactTriggerHandler.onAfterDelete(Trigger.old);
    }
}


There's a lot wrong going on here, that's beyond the scope of this forum when you've not shown what you've tried to fix it. Briefly: 1. As CloudConnect says, Contacts aren't tied to an Account. 2. You're not deleting any Contacts, which is why your handler gets no coverage. 3. Your assert compares the number of queried accounts to your static variable, which is why it passes, but what it asserts has nothing to do with the method you're testing.

You need to pass AccountId to Contacts created in test class.

contacts.add(new Contact(LastName='ContactLname' +i, AccountId =
Account1.Id));


Hey CloudConnect, I made the changes that you suggested. However, I'm still got getting  any code coverage in the onAfterDelete() method in the Handler. Do I need to delete a contact in the test data to activate the code coverage?

Yes, I missed that. Output of answering the question too quickly. Adding a delete contact in test class should cover onAfterDelete method too.

No worries. All lines are covered except 1 which is a.Number_of_Contacts__c = a.Contacts.size() in the For Loop in the Handler. What am I missing in the updateAccountAfterDelete() test method to pick up this line?

Contact deleted should have an account associated with it

Thx so much!! I have 100% code coverage now. I was way overthinking this and it seems rather simple now that it's done. Appreciate your insight!
