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
AnonymouseAnonymouse 

How to Make Test Class More Efficient With Bulkify

Hello,

I'm currently working on a Trailhead Challenge called: Create an Apex class that uses the @future annotation to update Account records.

I do not have any errors; however, how may I make my test class more efficient with bulkifying? I wish to create anywhere from 1-200 accounts/contacts for these tests. This is my current code:

 
@isTest
private class AccountProcessorTest {
	
    @isTest
    private static void testSingleAccountProcessor() {
        
        double numOfAcctsCreated = 0;
        
        // Create Account(s)
        Account acct = new Account(Name = 'Smith Hardward Account');
        insert acct; // This is not part of test
        
        // Create and insert Contact with Account ID
        Contact cont = new Contact(LastName = 'Smith',
                                   AccountId = acct.Id);
        insert cont;
        
        // Create List of Ids to keep track of Account Ids
        List<Id> listOfAcctIds = new List<Id>();
        
        // Add Account Id from Account just created into List
        listOfAcctIds.add(acct.Id);
        
        // Increment count on Account(s) created
        numOfAcctsCreated += 1;
        
        // Test
        Test.startTest();
        	AccountProcessor.countContacts(listOfAcctIds);
        Test.stopTest();
        
        // Verify results
        Account queryResults = [SELECT Number_of_Contacts__c
                                FROM Account
                                WHERE id =: acct.Id];
        
        
        System.assert(queryResults.Number_of_Contacts__c == numOfAcctsCreated,
                     'Error! Number of Contacts for Account failed. ' +
                     'queryResults.Number_of_Contacts__c = ' + 
                     queryResults.Number_of_Contacts__c + ' and ' +
                     'numOfAcctsCreated = ' + numOfAcctsCreated);
    }
    
    @isTest
    private static void testMultipleAccountProcessors() {
        
        double numOfAcctsCreated = 0;
        
        // Create Account(s)
        Account acct1 = new Account(Name = 'Smith Hardward Account1');
        insert acct1; // This is not part of test
        
        Account acct2 = new Account(Name = 'Smith Hardward Account2');
        insert acct2; // This is not part of test
        
        Account acct3 = new Account(Name = 'Smith Hardward Account3');
        insert acct3; // This is not part of test
        
        // Create and insert Contact with Account ID
        Contact cont1 = new Contact(LastName = 'Smith1',
                                   AccountId = acct1.Id);
        insert cont1;
        
        Contact cont2 = new Contact(LastName = 'Smith2',
                                   AccountId = acct2.Id);
        insert cont2;
        
        Contact cont3 = new Contact(LastName = 'Smith3',
                                   AccountId = acct3.Id);
        insert cont3;
        
        // Create List of Ids to keep track of Account Ids
        List<Id> listOfAcctIds = new List<Id>();
        
        // Add Account Id from Account just created into List
        listOfAcctIds.add(acct1.Id);
        listOfAcctIds.add(acct2.Id);
        listOfAcctIds.add(acct3.Id);
        
        // Increment count on Account(s) created
        numOfAcctsCreated = listOfAcctIds.size();
        
        // Test
        Test.startTest();
        	AccountProcessor.countContacts(listOfAcctIds);
        Test.stopTest();
        
        // Verify results
        Account queryResults1 = [SELECT Number_of_Contacts__c
                                FROM Account
                                WHERE id =: acct1.Id];
        
        Account queryResults2 = [SELECT Number_of_Contacts__c
                                FROM Account
                                WHERE id =: acct2.Id];
        
        Account queryResults3 = [SELECT Number_of_Contacts__c
                                FROM Account
                                WHERE id =: acct3.Id];
        
        Decimal counter =  	queryResults1.Number_of_Contacts__c + 
            				queryResults2.Number_of_Contacts__c +
            				queryResults3.Number_of_Contacts__c;
        
        System.assert(counter == numOfAcctsCreated,
                     'Error! Number of Contacts for Account failed. ' +
                     'counter = ' + counter + ' and ' +
                     'numOfAcctsCreated = ' + numOfAcctsCreated);
    }
}
Any help would be greatly appreciated.

Sincerely,
Jackie
Best Answer chosen by Anonymouse
Raj VakatiRaj Vakati
Yes .. You need to query and do it like below 
 
List<Account> queryResults3 = [SELECT Number_of_Contacts__c
                              FROM Account
                             ];

							   System.assert(200 == queryResults3.size()),
 
private static void testSingleAccountProcessor() {
        
        double numOfAcctsCreated = 0;
        
        // Create Account(s)
        List<Account> accws = new List<Account>();
		for(Integer i = 0 ;i <200 ;i++){
		Account acct = new Account(Name = 'Smith Hardward Account'+i);
		accws.add(acct) ; 
		}
		
		
        insert accws; // This is not part of test
        
		Map<Id, Account> accMap = new Map<Id,Account>([Select Id from Account]) ; 
		
		List<ID> accsIdList = new List<ID>();
		accsIdList.addAll(accMap.keySet());
		
        // Create and insert Contact with Account ID
		
		   List<Contact> connnList = new List<Contact>();
	
		for(Integer i = 0 ;i <200 ;i++){
		
        Contact cont = new Contact(LastName = 'Smith',
                                   AccountId = accsIdList.get(i));
								   connnList.add(con);
		}
        insert connnList;
        
        // Create List of Ids to keep track of Account Ids
       // List<Id> listOfAcctIds = new List<Id>();
        
        // Add Account Id from Account just created into List
        //listOfAcctIds.add(acct.Id);
        
        // Increment count on Account(s) created
       // numOfAcctsCreated += 1;
        
        // Test
        Test.startTest();
        	AccountProcessor.countContacts(accsIdList);
        Test.stopTest();
		
		 List<Account> queryResults3 = [SELECT Number_of_Contacts__c
                              FROM Account
                             ];

							   System.assert(200 == queryResults3.size()),

        
        // Verify results
      }

 

All Answers

Raj VakatiRaj Vakati
see the below sample code
 
@isTest
    private static void testSingleAccountProcessor() {
        
        double numOfAcctsCreated = 0;
        
        // Create Account(s)
        List<Account> accws = new List<Account>();
		for(Integer i = 0 ;i <200 ;i++){
		Account acct = new Account(Name = 'Smith Hardward Account'+i);
		accws.add(acct) ; 
		}
		
		
        insert accws; // This is not part of test
        
		Map<Id, Account> accMap = new Map<Id,Account>([Select Id from Account]) ; 
		
		List<ID> accsIdList = new List<ID>();
		accsIdList.addAll(accMap.keySet());
		
        // Create and insert Contact with Account ID
		
		   List<Contact> connnList = new List<Contact>();
	
		for(Integer i = 0 ;i <200 ;i++){
		
        Contact cont = new Contact(LastName = 'Smith',
                                   AccountId = accsIdList.get(i));
								   connnList.add(con);
		}
        insert connnList;
        
        // Create List of Ids to keep track of Account Ids
        List<Id> listOfAcctIds = new List<Id>();
        
        // Add Account Id from Account just created into List
        listOfAcctIds.add(acct.Id);
        
        // Increment count on Account(s) created
        numOfAcctsCreated += 1;
        
        // Test
        Test.startTest();
        	AccountProcessor.countContacts(accsIdList);
        Test.stopTest();
        
        // Verify results
      }

 
Raj VakatiRaj Vakati
private static void testSingleAccountProcessor() {
        
        double numOfAcctsCreated = 0;
        
        // Create Account(s)
        List<Account> accws = new List<Account>();
		for(Integer i = 0 ;i <200 ;i++){
		Account acct = new Account(Name = 'Smith Hardward Account'+i);
		accws.add(acct) ; 
		}
		
		
        insert accws; // This is not part of test
        
		Map<Id, Account> accMap = new Map<Id,Account>([Select Id from Account]) ; 
		
		List<ID> accsIdList = new List<ID>();
		accsIdList.addAll(accMap.keySet());
		
        // Create and insert Contact with Account ID
		
		   List<Contact> connnList = new List<Contact>();
	
		for(Integer i = 0 ;i <200 ;i++){
		
        Contact cont = new Contact(LastName = 'Smith',
                                   AccountId = accsIdList.get(i));
								   connnList.add(con);
		}
        insert connnList;
        
        // Create List of Ids to keep track of Account Ids
       // List<Id> listOfAcctIds = new List<Id>();
        
        // Add Account Id from Account just created into List
        //listOfAcctIds.add(acct.Id);
        
        // Increment count on Account(s) created
       // numOfAcctsCreated += 1;
        
        // Test
        Test.startTest();
        	AccountProcessor.countContacts(accsIdList);
        Test.stopTest();
        
        // Verify results
      }

This is correct code
 
AnonymouseAnonymouse
Thank you Raj. How about the assertion of the bulkified code though? How do you bulkify the querying? 
Raj VakatiRaj Vakati
Yes .. You need to query and do it like below 
 
List<Account> queryResults3 = [SELECT Number_of_Contacts__c
                              FROM Account
                             ];

							   System.assert(200 == queryResults3.size()),
 
private static void testSingleAccountProcessor() {
        
        double numOfAcctsCreated = 0;
        
        // Create Account(s)
        List<Account> accws = new List<Account>();
		for(Integer i = 0 ;i <200 ;i++){
		Account acct = new Account(Name = 'Smith Hardward Account'+i);
		accws.add(acct) ; 
		}
		
		
        insert accws; // This is not part of test
        
		Map<Id, Account> accMap = new Map<Id,Account>([Select Id from Account]) ; 
		
		List<ID> accsIdList = new List<ID>();
		accsIdList.addAll(accMap.keySet());
		
        // Create and insert Contact with Account ID
		
		   List<Contact> connnList = new List<Contact>();
	
		for(Integer i = 0 ;i <200 ;i++){
		
        Contact cont = new Contact(LastName = 'Smith',
                                   AccountId = accsIdList.get(i));
								   connnList.add(con);
		}
        insert connnList;
        
        // Create List of Ids to keep track of Account Ids
       // List<Id> listOfAcctIds = new List<Id>();
        
        // Add Account Id from Account just created into List
        //listOfAcctIds.add(acct.Id);
        
        // Increment count on Account(s) created
       // numOfAcctsCreated += 1;
        
        // Test
        Test.startTest();
        	AccountProcessor.countContacts(accsIdList);
        Test.stopTest();
		
		 List<Account> queryResults3 = [SELECT Number_of_Contacts__c
                              FROM Account
                             ];

							   System.assert(200 == queryResults3.size()),

        
        // Verify results
      }

 
This was selected as the best answer
AnonymouseAnonymouse
Hi Raj,

Thank you!

Sincerely,
Jackie