You need to sign in to do that
Don't have an account?
Sabarish Chandramouli 1
Future Method Test Class not Asserting
I am working on a trail for future method, my method finds the number of contacts on account and updates the "Number of Contacts" field on the Account.
Everyhting is working as expected but in the test class while asserting the output, the SOQL is not pulling this field.
Future Method:
Test Method:
At line 73: is wehre, I am not able to pull the Number_Of_Contacts field from Account. It is pulling the ID and Match Billing Address field.
Any help would be appreciated.
Everyhting is working as expected but in the test class while asserting the output, the SOQL is not pulling this field.
Future Method:
public class AccountProcessor { @future public static void countContacts(List<Id> AccIds){ List<Account> acctsContacts = new List<Account>(); //List<Contact> noOfContacts = new List<Contact>(); //integer noOfContacts; for(ID accn: AccIds){ //contactsID = [Select ID from Contact where Account.ID = :accn]; //noOfContacts = [Select COUNT() from Contact where Account.ID = :accn]; acctsContacts = [Select Name, (Select Firstname, Lastname FROM Contacts) FROM Account WHERE ID =:accn]; for(Account accns: acctsContacts){ integer noOfContacts = accns.Contacts.size(); System.debug('Number of Contacts' +noOfContacts); accns.Number_of_Contacts__c = noOfContacts; } } } }
Test Method:
@isTest public class AccountProcessorTest { @isTest public static void testcountContacts(){ //Test Data Preparation - Create new accounts ID testAccID; List<ID> testAccIdsList = new List<ID>(); List<Account> testAccount = new List<Account>{ new Account(Name ='TestAccount 1'), new Account(Name = 'TestAccount 2'), new Account(Name = 'TestAccount 3'), new Account(Name = 'TestAccount 4') }; insert testAccount; //Create 2 contact for each Account List<Contact>consList = new List<Contact>(); for (Account eachAccount:testAccount){ for(integer i=0; i<2; i++){ // List<Contact>consList = new List<Contact>{ Contact cons = new Contact(Firstname ='Contact'+i, Lastname = 'Test'+i, AccountId = eachAccount.Id); consList.add(cons); } } insert conslist; System.debug('Number of Contacts'+conslist.size()); System.debug('Contacts'+conslist); //Getting the IDs of the Accounts for(Account testaccn:testAccount){ //testAccId = [Select ID from Account //WHERE Account.ID =:testaccn.Id]; testAccIdsList.add(testaccn.ID); } //Test Starts Test.startTest(); AccountProcessor.countContacts(testAccIdsList); Test.stopTest(); List<Account>accContactPresent = new list<Account>(); System.debug('List of IDs'+testAccIdsList); for(Id eachaccID :testAccIdsList){ // This SOQL is not pulling the Number_of_Contacts field Account contactCount = [Select Match_Billing_Address__c, Number_of_Contacts__c from Account WHERE ID= :eachaccID]; accContactPresent.add(contactCount); } for(Account foreachContact :accContactPresent){ System.debug('Contacts Present'+accContactPresent); System.assertEquals(2, foreachContact.Number_of_Contacts__c); } } }
At line 73: is wehre, I am not able to pull the Number_Of_Contacts field from Account. It is pulling the ID and Match Billing Address field.
Any help would be appreciated.
You are missing update DML operation in your future method , you need to update the records to store the counts in database account object.
Hope this helps.
All Answers
You are missing update DML operation in your future method , you need to update the records to store the counts in database account object.
Hope this helps.
I Think You Only Update Last 'acctsContacts' with Account.ID = :accn.
I want To update Whole 'acctsContacts' with Account.ID IN :AccIds. which 'AccIds' List I passed through method parameter.