+ Start a Discussion
SFTerrSFTerr 

Assistance with a test class to increase code coverage

Hi, I'm struggling with test class to increase test coverage, currently got only 68%, so all suggestions welcome

my trigger:
/* Provide summary of Number of Contacts on Account record
https://success.salesforce.com/answers?id=90630000000h3mNAAQ */ 

trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, 
after update) {

    Contact[] cons;
    if (Trigger.isDelete) 
        cons = Trigger.old;
    else
        cons = Trigger.new;

    // get list of accounts
    Set<ID> acctIds = new Set<ID>();
    for (Contact con : cons) {
            acctIds.add(con.AccountId);
    }
    
    Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
                                                            ,AccountId
                                                            from Contact
                                                            where AccountId in :acctIds]);

    Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
                                                                 ,Number_of_Contacts__c
                                                                  from Account
                                                                  where Id in :acctIds]);
                                                                 
    for (Account acct : acctsToUpdate.values()) {
        Set<ID> conIds = new Set<ID>();
        for (Contact con : contactsForAccounts.values()) {
            if (con.AccountId == acct.Id)
                conIds.add(con.Id);
        }
        if (acct.Number_of_Contacts__c != conIds.size())
            acct.Number_of_Contacts__c = conIds.size();
    }

    update acctsToUpdate.values();

}

my class:
 
@isTest
public class ContactSumTriggerTest {

    static testmethod void TestContactTrgr() {
    
           Test.startTest();
        // Create account
           Account ac = new Account();
           ac.Name = 'Test Account';
           ac.Mailing_Country__c = 'United Kingdom';
           Insert ac;
           system.debug('Completed Account Creation'); 
		   
		// Update account
          Database.SaveResult sr2 = Database.update(ac, true);
          System.assert(sr2.isSuccess());
          system.debug('Account updated');

           delete ac;
           undelete ac;
		   
        // Create 1st contact
           Contact ct1 = new Contact();
           ct1.FirstName = 'FirstName 1';
           ct1.LastName = 'LastName 1';
           ct1.Email = 'test1@test.com';
           ct1.Mailing_Country__c = 'United Kingdom';
           ct1.Account = ac;
           Insert ct1;  
		   system.debug('Completed Contact Creation'); 
		   
		// Update 1st contact
          Database.SaveResult sr2 = Database.update(ct1, true);
          System.assert(sr2.isSuccess());
          system.debug('Contact updated');
		   
		   delete ct1;
           undelete ct1;
		   
		// Create 2nd contact
		   Contact ct2 = new Contact();
           ct2.FirstName = 'FirstName 2';
           ct2.LastName = 'LastName 2';
           ct2.Email = 'test2@test.com';
           ct2.Mailing_Country__c = 'United Kingdom';
           ct2.Account = ac;
           Insert ct2;
           system.debug('Completed Contact Creation'); 
		   
		// Update 2nd contact
          Database.SaveResult sr2 = Database.update(ct2, true);
          System.assert(sr2.isSuccess());
          system.debug('Contact updated');
           
		   delete ct2;
           undelete ct2;
           
           Test.stopTest();
          
    }    
    
}

thank you in advance
Best Answer chosen by SFTerr
abirla19abirla19
@isTest
public class ContactSumTriggerTest {

    static testmethod void TestContactTrgr() {
        
        Test.startTest();
        
        List<Account> accountList = new List<Account>();
        
        Account acc = new Account();
        acc.Name = 'Jade Global';
        acc.Number_of_Contacts__c = 3 ;
        
        accountList.add(acc);
        
        if(!accountList.isEmpty()){
            insert accountList;
        }
        
        Set<Id> actIdSet = new Set<Id>();
        
        if(!accountList.isEmpty()){
             for(Account acct : accountList){
              actIdSet.add(acct.Id);
            }
        }
        
        List<Contact> contactList = new List<Contact>();
        
        Contact con1 = new Contact();
        con1.firstname = 'Ankur';
        con1.lastname = 'Birla';
        con1.AccountId = accountList[0].Id;
        con2.Mailing_Country__c ='Asia';

        Contact con2 = new Contact();
        con2.firstname = 'Salesforce';
        con2.lastname = 'Developer';
        con2.AccountId = accountList[0].Id;
        con2.Mailing_Country__c ='India';
        
        contactList.add(con1);
        contactList.add(con2);
        
        if(!contactList.isEmpty()){
            insert contactList;
        }
        
        Delete con2;

        Test.stopTest();
        
    }    
    
}


You can simply use this code snippet. I have put the above code to 'Run Test' and it is already showing the Code Coverage as 100%.

Mark the answer as Solution so that developers browsing around can look directly to the answer that provides a solution.

Cheers Mate ! :)

All Answers

Abhi_TripathiAbhi_Tripathi
Please go to the developer console, and check which line of your trigger is not covered from the your test class, then perform same scenario in your test class.
abirla19abirla19
@isTest
public class ContactSumTriggerTest {

    static testmethod void TestContactTrgr() {
        
        Test.startTest();
        
        List<Account> accountList = new List<Account>();
        
        Account acc = new Account();
        acc.Name = 'Jade Global';
        acc.Number_of_Contacts__c = 3 ;
        
        accountList.add(acc);
        
        if(!accountList.isEmpty()){
            insert accountList;
        }
        
        Set<Id> actIdSet = new Set<Id>();
        
        if(!accountList.isEmpty()){
             for(Account acct : accountList){
              actIdSet.add(acct.Id);
            }
        }
        
        List<Contact> contactList = new List<Contact>();
        
        Contact con1 = new Contact();
        con1.firstname = 'Ankur';
        con1.lastname = 'Birla';
        con1.AccountId = accountList[0].Id;
        con2.Mailing_Country__c ='Asia';

        Contact con2 = new Contact();
        con2.firstname = 'Salesforce';
        con2.lastname = 'Developer';
        con2.AccountId = accountList[0].Id;
        con2.Mailing_Country__c ='India';
        
        contactList.add(con1);
        contactList.add(con2);
        
        if(!contactList.isEmpty()){
            insert contactList;
        }
        
        Delete con2;

        Test.stopTest();
        
    }    
    
}


You can simply use this code snippet. I have put the above code to 'Run Test' and it is already showing the Code Coverage as 100%.

Mark the answer as Solution so that developers browsing around can look directly to the answer that provides a solution.

Cheers Mate ! :)
This was selected as the best answer
SFTerrSFTerr
Thank you very much for all your help guys, and abirla19 for the actual class, works great, much appreciated.

I wasn't aware that in dev console you can check which part of the clas is not covered, lessonfor me that notepad ++ is not always the best tool