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
Priya 21Priya 21 

How to increase this test class code coverage to 100%

Hi All,
 Can anyone help me to increase my test class code coverage to 100%, This is my first test class, Now my test class coverage is 85%, I want to increase the code coverage to 100%, What are the changes i need to do in my test class to get code coverage of 100%?

My Apex Class :

public class UpdateContactAddress {
    public void  updateContact(List<ID> contactId){
        List<Contact> contactSO = [Select id,MailingAddress,AccountId from Contact Where id=:contactId];
        List<Contact> contactList = new List<Contact>();
            for(Contact con : contactSO){
                //for(Account acc: [Select BillingCity,BillingStreet from Account where id=:con.AccountId]){
                       Account account = [Select BillingCity,BillingStreet from Account where id=:con.AccountId];
                        con.MailingCity  = account.BillingCity;
                       con.MailingStreet = account.BillingStreet;
                    contactList.add(con);
                //}
                }
         update contactList;
    }
}
Trigger Code:
trigger IFAddrChangedUpdateContact on Account (after Update) {
    List<Id> contactIds = new List<Id>();
    List<Id> accountIds = new List<Id>();
    for(Account acc: Trigger.New){
    for(Contact con : acc.Contacts){
          contactIds.add(con.id);
    }
    }
    UpdateContactAddress obj = new UpdateContactAddress();
        obj.updateContact(contactIds);
}
Test Method:
@isTest
public class TestUpdateContactAddress {
    @isTest static void TestupdateContact() {
        Test.startTest();
        Account Acc1 = new Account(Name = 'Acc1 by Test',BillingCity = 'Chennai',BillingStreet = 'Lakeview Street');
        insert Acc1;
         Account Acc2 = new Account(Name = 'Acc2 by Test',BillingCity = 'Chennai',BillingStreet = 'Lakeview Street');
        insert Acc2;
        Contact con1 = new Contact(LastName='Contact created using test1', AccountId=Acc1.Id);
        Contact con2 = new Contact(LastName='Contact created using test2', AccountId=Acc1.Id);
        insert con2;
        insert con1;
         Contact con3 = new Contact(LastName='Contact created using test1', AccountId=Acc2.Id);
        Contact con4 = new Contact(LastName='Contact created using test2', AccountId=Acc2.Id);
        insert con3;
        insert con4;
        Account account1 = [Select Id,BillingCity,(SELECT ID from Contacts) from Account Where id =: Acc1.Id];
        account1.BillingCity='Vellore';
        update account1;
        Account account2 = [Select Id,BillingCity,(SELECT ID from Contacts) from Account Where id =: Acc1.Id];
        account2.BillingCity='Bangalore City';
        update account2;
        Test.stopTest();
    }
}
 

In the trigger code, I got only 85% code coverage and my apex class covered 100%. It shows,  contactIds.add(con.id);  this line is not covered.Please help me to cover my trigger code to 100%. 

Best Answer chosen by Priya 21
AnudeepAnudeep (Salesforce Developers) 
Hi Varshini, 

There isn't any problem with your test class. The issue lies in your main class. I am able to get 100% code coverage. Use the below code

IFAddrChangedUpdateContact
trigger IFAddrChangedUpdateContact on Account (after Update) {
    List<Id> contactIds = new List<Id>();
    List<Id> accountIds = new List<Id>();
    for(Account acc: [Select id,(select id from contacts) from account]){
    for(Contact con : acc.Contacts){
          contactIds.add(con.id);
    }
    }
    UpdateContactAddress obj = new UpdateContactAddress();
        obj.updateContact(contactIds);
}

If you find this information helpful, please mark this answer as Best. Thank You!

Anudeep

 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Varshni,

By default we would be needing 1 percent for trigger and 75 percent code coverage for apex classes and it is generally not preferred to write a soql query in a loop as it could hit governed limit, so you could fetch them before hand manipute to get them in a map and then update the contact address.

Also, you could check by putting a debug statement if you are getting correct records, it could be that there are no records in the list so the loop might not be executing.

I hope this helps.

Regards,
Anutej
AnudeepAnudeep (Salesforce Developers) 
Hi Varshini, 

There isn't any problem with your test class. The issue lies in your main class. I am able to get 100% code coverage. Use the below code

IFAddrChangedUpdateContact
trigger IFAddrChangedUpdateContact on Account (after Update) {
    List<Id> contactIds = new List<Id>();
    List<Id> accountIds = new List<Id>();
    for(Account acc: [Select id,(select id from contacts) from account]){
    for(Contact con : acc.Contacts){
          contactIds.add(con.id);
    }
    }
    UpdateContactAddress obj = new UpdateContactAddress();
        obj.updateContact(contactIds);
}

If you find this information helpful, please mark this answer as Best. Thank You!

Anudeep

 
This was selected as the best answer
Footprints on the MoonFootprints on the Moon
Hi Varshni,

Update your trigger code using below snippet-
Apparently this will solve loop inside loop issue (not a best practice)
//Set of account IDs from Trigger.new
Set<Id> accIdSet = new Set<Id>();
for(Account acc : Trigger.new){
	accIdSet.add(acc.Id);
}

//Fetch only those contacts related to updated accounts
List<Contact> cList = [select Id from Contact where AccountId in :accIdSet];
for(Contact c : cList){
	contactIds.add(c.Id);
}