0

Yeah this might be a simple question. As I am a beginner, I would like to know how to cover 100% code coverage for the below small trigger. Trigger:

trigger UpdateChildRecord on Account (before insert, before update) {

    List<Contact> conList = new List<Contact> (); 
    for (Contact c: [SELECT NAME,ID,PHONE FROM CONTACT WHERE AccountId IN: Trigger.new]){
        c.Phone='+91-12345678';
        conList.add(c);
    }
    update conList;
}

I'm able to cover 60% as in the below Test Class

@isTest
public class UpdateChildRecordTestClass {
    @isTest
    Static void testUpdateChildRecordTrigger(){
        Account a = new Account (Name='TestClass', Industry='IT');
        insert a;
        Contact c = new Contact (LastName='Last'+a.Name+'Contact',FirstName='First'+a.Name+'Contact',AccountId=a.Id);
        insert c;
        System.assertEquals(c.Phone, '+91-12345678');
    }  
}

Please help me in writing 100% code coverage test class

3

As you have created trigger on Before insert and before update and trigger code you are looking for Contacts under the account record. In case of Account insert you will not have any child contacts under it so as per your current test class you will have coverage only 60%.

Your for loop will come in execution only when you will update an existing Account record that has any Contact record under it.

You have to put an Account update statement in your test class as:

@isTest
public class UpdateChildRecordTestClass {
    @isTest
    Static void testUpdateChildRecordTrigger(){
        Account a = new Account (Name='TestClass', Industry='IT');
        insert a;
        Contact c = new Contact (LastName='Last'+a.Name+'Contact',FirstName='First'+a.Name+'Contact',AccountId=a.Id);
        insert c;

        update a;

        // query updated contact to get latest phone number from contact
        Contact updatedContact = [SELECT Id, Phone FROM Contact WHERE Id=:c.Id];
        // Verify updated phone number here.
        System.assertEquals(updatedContact.Phone, '+91-12345678');
    }  
}
| improve this answer | |
  • This unit test will fail the assertion because the contact was not queried back from the database. – sfdcfox Aug 20 '17 at 6:46
  • Thanks for the correction @sfdcfox :) I made an update to my answer. – thatherahere Aug 20 '17 at 6:54
1

Your trigger is not doing anything because when it runs the Account does not have any Contacts tied to it. Easiest way to get it fixed would to do an update on the Account object after you insert the Contact.

@isTest
public class UpdateChildRecordTestClass {
    @isTest
    Static void testUpdateChildRecordTrigger(){
        Account a = new Account (Name='TestClass', Industry='IT');
        insert a;
        Contact c = new Contact (LastName='Last'+a.Name+'Contact',FirstName='First'+a.Name+'Contact',AccountId=a.Id);
        insert c;
        update a; //Added to cause trigger to rerun
        c = [Select phone from contact where id :c.id];
        System.assertEquals(c.Phone, '+91-12345678');
    }  
}
| improve this answer | |
  • This unit test will fail the assertion because the contact was not queried back from the database. – sfdcfox Aug 20 '17 at 6:46
  • @sfdcfox valid point updated – Halfwarr Aug 20 '17 at 6:57
0

In query, you are using WHERE AccountId IN: Trigger.new, and in before Insert, AccountId will be null. So every time your before insert trigger will do nothing, and you can remove the before insert in first line of code.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.