You need to sign in to do that
Don't have an account?

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%.
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
If you find this information helpful, please mark this answer as Best. Thank You!
Anudeep
All Answers
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
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
If you find this information helpful, please mark this answer as Best. Thank You!
Anudeep
Update your trigger code using below snippet-
Apparently this will solve loop inside loop issue (not a best practice)