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
Yelper DevYelper Dev 

Account and Contact Create Test Class

Hi  need help on creating test class for the code below: 
public static void copyContactMailingAddress(List<Account> acctList, Map<Id, Account> oldAcctMap){
        //Returns all new keys contain in the oldAcctMap
        Set<Id> acctNewId = Trigger.newMap.keyset();
        for (Account acct : acctList){
            Account oldAcctRecord = oldAcctMap.get(acct.Id);
            //Query all contactRecords with their mailing address
            List<Contact> contactRecord = [SELECT id, accountId, MailingStreet, MailingCity, MailingCountry, MailingPostalCode, MailingState
                                           FROM Contact
                                           WHERE accountId in : acctNewId]; 
            
            //Compare values from old to new billing address.
            for (Contact con : contactRecord) {   
                if (oldAcctRecord.BillingStreet != acct.BillingStreet
                   || oldAcctRecord.BillingCity != acct.BillingCity
                   || oldAcctRecord.BillingState != acct.BillingState
                   || oldAcctRecord.BillingPostalCode != acct.BillingPostalCode
                   || oldacctRecord.BillingCountry != acct.BillingCountry){
                    con.MailingStreet = acct.BillingStreet;
                    con.MailingCity = acct.BillingCity;
                    con.MailingState = acct.BillingState;
                    con.MailingPostalCode = acct.BillingPostalCode;
                    con.MailingState = acct.BillingCountry;
                }
           }
            //DML Statement to update the Billing Address that is in the Account to all related contacts
            update  contactRecord;
        }       
    }

The trigger is in before update, already have test factory for this:
@isTest
public TestDataFactory{
public static List<Account> createAcctWithCon(Integer numAcct, Integer numConPerAcct){
        List<Account> acct = new List<Account>();
        
        for(Integer i = 0; i < numAcct; i++){
            Account a = new Account(Name = 'Test Account' + i);
            acct.add(a);
        }
        insert acct; 
        
        List<Contact> con = new List<Contact>();
        for(Account at : acct){
            for(Integer j = 0; j < numAcct; j++){
                con.add(new Contact (LastName = at.Name + 'Contact' + j, AccountId = at.Id));
            }
        }
        insert con;
        return acct;
    }
}

Much need help thank you!
Best Answer chosen by Yelper Dev
Raj VakatiRaj Vakati
change code like below
 
@isTest static void copyContactMailingAddressTest(){
        List<Account> tempAcctList = new List<Account>();	
        Account acct = TestDataFactory.getSingleAccount(1);
        acct.BillingStreet = '3022  Strother Street';
        acct.BillingCity = 'Into';
        acct.BillingState = 'Alabama';
        acct.BillingPostalCode = '35222';
        acct.BillingCountry = 'US';
        tempAcctList.add(acct);
        insert tempAcctList;
        
        Test.startTest();
        for(Account acctUpdate : tempAcctList){
            acctUpdate.BillingStreet = '';
            acctUpdate.BillingCity = '';
            acctUpdate.BillingState = 'Edmonton';
            acctUpdate.BillingPostalCode = '1232';
            acctUpdate.BillingCountry = 'Canada';            
            update acctUpdate;  
        }		      
        Test.stopTest();
        
        List <Contact> conList = [SELECT Id, AccountId, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry FROM Contact WHERE AccountId =: tempAcctList[0].Id];
        for(Contact updateCon : conList){
         //Stuck up here for assertion. Not sure what's next
        }        
    }

 

All Answers

Raj VakatiRaj Vakati
Try like this
 
@isTest
public TestDataFactory{
public static List<Account> createAcctWithCon(Integer numAcct, Integer numConPerAcct){
        List<Account> acct = new List<Account>();
        
        for(Integer i = 0; i < numAcct; i++){
            Account a = new Account(BillingStreet = 'Am Tierpark 16',
BillingCity = 'Cologne',
BillingPostalCode = '54321',
BillingState = 'Nordrhein-Westfalen',
BillingCountry = 'Germany' ,Name = 'Test Account' + i);
			
            acct.add(a);
        }
        insert acct; 
        
        List<Contact> con = new List<Contact>();
        for(Account at : acct){
            for(Integer j = 0; j < numAcct; j++){
                con.add(new Contact (LastName = at.Name + 'Contact' + j, AccountId = at.Id));
            }
        }
        insert con;
        return acct;
    }
}

And make sure you need to update the record in the test class after you get from the factory class
Yelper DevYelper Dev
HI Raj, as per best practice don't do DML statements in TestDataFActory since it we will just add some temporary data. However, I just created one account and contact in data and return its value to the variables created. I already have a draft on test class, but still stuck on what to do next.
 
@isTest static void copyContactMailingAddressTest(){
        List<Account> tempAcctList = new List<Account>();	
        Account acct = TestDataFactory.getSingleAccount(1);
        acct.BillingStreet = '3022  Strother Street';
        acct.BillingCity = 'Into';
        acct.BillingState = 'Alabama';
        acct.BillingPostalCode = '35222';
        acct.BillingCountry = 'US';
        tempAcctList.add(acct);
        insert tempAcctList;
        
        Test.startTest();
        for(Account acctUpdate : tempAcctList){
            acctUpdate.BillingStreet = '';
            acctUpdate.BillingCity = '';
            acctUpdate.BillingState = 'Edmonton';
            acctUpdate.BillingPostalCode = '1232';
            acctUpdate.BillingCountry = 'Canada';            
            update acctUpdate;  
        }		      
        Test.stopTest();
        
        List <Contact> conList = [SELECT Id, AccountId, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry FROM Contact WHERE AccountId =: tempAcctList.Id];
        for(Contact updateCon : conList){
         //Stuck up here for assertion. Not sure what's next
        }        
    }

Also i am getting an error that I cant retreieve the accountId of tempAcctList
Raj VakatiRaj Vakati
change code like below
 
@isTest static void copyContactMailingAddressTest(){
        List<Account> tempAcctList = new List<Account>();	
        Account acct = TestDataFactory.getSingleAccount(1);
        acct.BillingStreet = '3022  Strother Street';
        acct.BillingCity = 'Into';
        acct.BillingState = 'Alabama';
        acct.BillingPostalCode = '35222';
        acct.BillingCountry = 'US';
        tempAcctList.add(acct);
        insert tempAcctList;
        
        Test.startTest();
        for(Account acctUpdate : tempAcctList){
            acctUpdate.BillingStreet = '';
            acctUpdate.BillingCity = '';
            acctUpdate.BillingState = 'Edmonton';
            acctUpdate.BillingPostalCode = '1232';
            acctUpdate.BillingCountry = 'Canada';            
            update acctUpdate;  
        }		      
        Test.stopTest();
        
        List <Contact> conList = [SELECT Id, AccountId, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry FROM Contact WHERE AccountId =: tempAcctList[0].Id];
        for(Contact updateCon : conList){
         //Stuck up here for assertion. Not sure what's next
        }        
    }

 
This was selected as the best answer
Raj VakatiRaj Vakati
Comment the system.assert or add the expected value based on the data ... 
Raj VakatiRaj Vakati
can u give me complete class anf test class
 
Raj VakatiRaj Vakati
Close this thread if its solved