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
123_P123_P 

Write Test Class for this Trigger Code

Handler--------------------------

public class UpdateAccAddressTriggerHandler{
    public static void changeAccAddress(list<contact> contList){
        set<Id> AcctIds=new set<Id>();
        list<Account> accList=new list<Account>();
        map<Id,Contact> contMap = new map<Id,Contact>();
        
        for(Contact con:contList){
            AcctIds.add(con.AccountId);       
        }
        
        for(Contact con:[Select Id,AccountId, MailingStreet,MailingCity, MailingState, MailingCountry,  MailingPostalCode,LastModifiedDate  from Contact 
        WHERE AccountId IN:AcctIds order by LastModifiedDate Desc limit 1]){
             if(!contMap.containskey(con.accountId)){
             contMap.put(con.AccountId,con); 
             }           
        }
        
        for(Account acc:[select id,BillingCity,BillingStreet,BillingCountry,BillingPostalCode from Account where id IN:AcctIds]){          
            Contact con =contMap.get(acc.id);
            acc.BillingStreet=con.MailingStreet; 
            acc.BillingCity=con.MailingCity;
            acc.BillingState=con.MailingState;
            acc.BillingPostalCode=con.MailingPostalCode;
            acc.BillingCountry=con.MailingCountry;                   
            accList.add(acc);
       }                
       update accList;        
   }
}

Trigger--------------------------
trigger updateAccAddressTrigger on Contact (after insert,after update) {
    if(Trigger.isAfter){
        UpdateAccAddressTriggerHandler.changeAccAddress(Trigger.New);
    
    }
}
Best Answer chosen by 123_P
Niraj Kr SinghNiraj Kr Singh
Hi Aarush,

Try this code:
Note: Might be some update needed.
@isTest
private class MyTestClass {
	static testMethod void accountDescriptionsInsertTest(){
		// Perform our data preparation.
		List<Account> accounts = new List<Account>{};
		List<Contact> contacts = new List<Contact>{};
			
		for(Integer i = 0; i < 5; i++){
			Account a = new Account(Name = 'Test Account ' + i);
			accounts.add(a);
		}
		 
		
		test.startTest();
			try{
				insert accounts;
				
				Integer index = 0;
				for(Account objAcc : accounts) {
					Contact objCon = new Contact(FirstName = 'Test', LastName = 'Con' + index, AccountId = objAcc.Id, MailingStreet = 'Street', MailingCity = 'City', MailingState = 'State', MailingPostalCode = '23105', MailingCountry = 'Country'); //Add other required field here if needed.
					contacts.add(objCon);
					index++;
				}
				insert contacts;
				
				List<Contact> lstContactIns = [Select Id From Contact];
				system.assertEquals(lstContactIns.size(), contacts.size());
			}
			catch(Exception ex){
				system.debug('----Exception----' + ex);
			}
		
		test.stopTest();
	}
	
	static testMethod void accountDescriptionsUpdateTest(){
		// Perform our data preparation.
		List<Account> accounts = new List<Account>{};
		List<Contact> contacts = new List<Contact>{};
			
		for(Integer i = 0; i < 5; i++){
			Account a = new Account(Name = 'Test Account ' + i);
			accounts.add(a);
		}
		 
		
		test.startTest();
			try{
				insert accounts;
				
				Integer index = 0;
				for(Account objAcc : accounts) {
					Contact objCon = new Contact(FirstName = 'Test', LastName = 'Con' + index, AccountId = objAcc.Id, MailingStreet = 'Street', MailingCity = 'City', MailingState = 'State', MailingPostalCode = '23105', MailingCountry = 'Country'); //Add other required field here if needed.
					contacts.add(objCon);
					index++;
				}
				insert contacts;
				
				//update
				for(Contact objCon : contacts) {
					objCon.LastName = objCon.LastName + 'Update';
				}
				Update contacts;
				
				List<Contact> lstContactIns = [Select Id From Contact Where LastName LIKE '%Update'];
				system.assertEquals(lstContactIns.size(), contacts.size());
			}
			catch(Exception ex){
				system.debug('----Exception----' + ex);
			}
		
		test.stopTest();
	}
}
If it works mark ur ans.

Thanks
Niraj