0

I test my class and its 50% on trigger handler class, what can I do to cover the lines with the red code;

My Class:

public class AccountTriggerHandler
{
    public static void updateContactAddresses(Map<ID, Account> updatedAccounts, Map<ID, Account> oldAccounts)
    {
        Set<Id> accountIds = new Set<Id>();
        for (Account a : updatedAccounts.values()){ 
            if(oldAccounts !=null) {
                //this is the red highlight
              /*  Account old = oldAccounts.get(a.Id); 
                if (a.billingstreet != old.billingstreet|| a.billingcity !=old.billingcity || a.billingstate !=old.billingstate || a.billingcountry != old.billingcountry) { 
                    accountIds.add(a.Id); */
                }
            } 
            else{
               accountIds.add(a.Id); 
            }
        }
        if (accountIds.size() > 0) {
            Contact[] updateAddress = [SELECT Id, accountId FROM contact WHERE accountId IN :accountIds];
            for (contact c : updateAddress) {
                 //red highlights
              /*  account a = updatedAccounts.get(c.accountId);
                c.mailingStreet = a.billingStreet;
                c.mailingcity = a.billingcity;
                c.mailingstate = a.billingstate;
                c.mailingpostalcode = a.billingpostalcode;
                c.mailingcountry = a.billingcountry;
               */
                }
            update updateAddress;
         }
    }
}

My trigger is 100%;

trigger contactFromAccounts on Account (after insert, after update) {
    AccountTriggerHandler.updateContactAddresses(Trigger.NewMap, Trigger.OldMap);      
}

This is my Test class:

@isTest
private class ContactAddressChange {
    static testmethod void testAddressChange() {
        //create Account
        Account sAcct = new Account();
        sAcct.Name = 'Test Account 1';
        //create address
        sAcct.BillingStreet = '11 Initial Street';
        sAcct.BillingCity = 'Rochester';
        sAcct.BillingPostalCode = '12456';
        sAcct.BillingState = 'NY';
        sAcct.BillingCountry = 'USA';
        insert sAcct;
        Test.stopTest();

        //create contact for Account "Test Account 1"
        Contact sContact1 = new Contact();
         //create contact address
        sContact1.MailingStreet = '1 Initial street';
        sContact1.MailingCity = 'Rochester';
        sContact1.MailingPostalCode = '12456';
        sContact1.MailingState = 'NY';
        sContact1.MailingCountry = 'USA';
        // sContact1.account.id = sAcct.id;
        sAcct.id = sContact1.account.id;

        Contact sContact2 = new Contact();
        sContact2.MailingStreet = '2 Initial street';
        sContact2.MailingCity = 'Rochester';
        sContact2.MailingPostalCode = '12456';
        sContact2.MailingState = 'NY';
        sContact2.MailingCountry = 'USA';
        //sContact2.account.id = sAcct.id;
        sAcct.id = sContact2.account.id;

        Contact sContact3 = new Contact();
        sContact3.MailingStreet = '3 Initial street';
        sContact3.MailingCity = 'Rochester';
        sContact3.MailingPostalCode = '12456';
        sContact3.MailingState = 'NY';
        sContact3.MailingCountry = 'USA';
        //sContact3.account.id = sAcct.id;
        sAcct.id = sContact3.account.id;

        //change address on account
        sAcct.BillingStreet = '11 Updated Street'; 
        sAcct.BillingCity = 'Updated City';
        sAcct.BillingPostalCode = '99999';
        sAcct.BillingState = 'PA';
        sAcct.BillingCountry = 'Mexico';

        Test.startTest();
         update sAcct;//this should fire my trigger.
        Test.stopTest();

        //check if account updated address match each contact address
        System.assertEquals(sAcct.BillingStreet, sContact1.mailingstreet); 
        System.assertEquals(sAcct.BillingCity, sContact1.MailingCity); 
        System.assertEquals(sAcct.BillingPostalCode, sContact1.MailingPostalCode); 
        System.assertEquals(sAcct.BillingState, sContact1.MailingState);
        System.assertEquals(sAcct.BillingCountry, sContact1.MailingCountry);

        System.assertEquals(sAcct.BillingStreet, sContact2.mailingstreet); 
        System.assertEquals(sAcct.BillingCity, sContact2.MailingCity); 
        System.assertEquals(sAcct.BillingPostalCode, sContact2.MailingPostalCode); 
        System.assertEquals(sAcct.BillingState, sContact2.MailingState);
        System.assertEquals(sAcct.BillingCountry, sContact2.MailingCountry);

        System.assertEquals(sAcct.BillingStreet, sContact3.mailingstreet); 
        System.assertEquals(sAcct.BillingCity, sContact3.MailingCity); 
        System.assertEquals(sAcct.BillingPostalCode, sContact3.MailingPostalCode); 
        System.assertEquals(sAcct.BillingState, sContact3.MailingState);
        System.assertEquals(sAcct.BillingCountry, sContact3.MailingCountry);
    }
}
  • Your class AccountTriggerHandler will not compile. – Reshma Jan 9 at 8:46
  • @Reshma What do you mean will not compile? – user63498 Jan 9 at 8:55
  • You have not commented one closing brace } – Reshma Jan 9 at 8:56
  • I just comment that here for stating that the lines are in red highlights. – user63498 Jan 9 at 9:01
  • Please also take a look at these resources. – David Reed Jan 9 at 13:50
1

I can see many issues with your code and test class.

  1. You have not commented closing brace } at line 12
  2. There are multiple Test.stopTest() in test class
  3. You have not updated contact's AccountId field so will not cover 50% of your code. Contact did not inserted to database.
  4. You will be getting required field error for Contact since Lastname was not populated.
  5. There will be update exception for Account since Id was not specified.

Update your test class with below code and add assertion.

@isTest
private class ContactAddressChange {
    static testmethod void testAddressChange() {
        Test.startTest();
        //create Account
        Account sAcct = new Account();
        sAcct.Name = 'Test Account 1';
        //create address
        sAcct.BillingStreet = '11 Initial Street';
        sAcct.BillingCity = 'Rochester';
        sAcct.BillingPostalCode = '12456';
        sAcct.BillingState = 'NY';
        sAcct.BillingCountry = 'USA';
        insert sAcct;

        //create contact for Account "Test Account 1"
        Contact sContact1 = new Contact();
        //create contact address
        sContact1.Lastname = 'sContact1';
        sContact1.MailingStreet = '1 Initial street';
        sContact1.MailingCity = 'Rochester';
        sContact1.MailingPostalCode = '12456';
        sContact1.MailingState = 'NY';
        sContact1.MailingCountry = 'USA';
        sContact1.Accountid = sAcct.id;

        Contact sContact2 = new Contact();
        sContact2.Lastname = 'sContact2';
        sContact2.MailingStreet = '2 Initial street';
        sContact2.MailingCity = 'Rochester';
        sContact2.MailingPostalCode = '12456';
        sContact2.MailingState = 'NY';
        sContact2.MailingCountry = 'USA';
        sContact2.Accountid = sAcct.id;

        Contact sContact3 = new Contact();
        sContact3.Lastname = 'sContact3';
        sContact3.MailingStreet = '3 Initial street';
        sContact3.MailingCity = 'Rochester';
        sContact3.MailingPostalCode = '12456';
        sContact3.MailingState = 'NY';
        sContact3.MailingCountry = 'USA';
        sContact3.Accountid = sAcct.id;

        List<Contact> contacts = new List<Contact>{sContact1, sContact2, sContact3};
        Insert contacts;

        //change address on account
        sAcct.BillingStreet = '11 Updated Street'; 
        sAcct.BillingCity = 'Updated City';
        sAcct.BillingPostalCode = '99999';
        sAcct.BillingState = 'PA';
        sAcct.BillingCountry = 'Mexico';

        update sAcct;//this should fire my trigger.
        Test.stopTest();

        //Add valid assertion for unit test
    }
}
  • Hi @Reshma, can you help me on my visualforce project? I have a post and I need some help. I very much appreciate if you help me..Thank you very much . – user63498 Jan 15 at 8:37

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.