Use Case:
Create a batch class to update Contacts:
- Description of Contact should be updated with
“Title of AccountName : Account’s Description“
- Contact which does not have any Account associated with it, should not be updated.
// Batch Class which implements Database.Batchableglobal class UpdateContactDescription Implements Database.Batchable<sObject> {// Getting all the contacts having some accountId populated using Query in start() methodglobal Database.QueryLocator start(Database.BatchableContext bc) {String query = 'Select Id, Account.Name, Title, Description, Account.Description From Contact Where AccountId != null';return Database.getQueryLocator(query);}// Go through the each Contact and update the Contact's descriptionglobal void execute(Database.BatchableContext bc, List<Contact> scope) {if(scope != null) {for(Contact con : scope) {con.Description = con.Title + ' of ' + con.Account.Name + ': ' + con.Account.Description;}update scope;}}// There is nothing as such required in finish method is it is empty but any post process step can be added hereglobal void finish (Database.BatchableContext bc) {}}
Test Class:
Create Accounts (with some description) and Contacts associated with the Account (without blank description).
Execute the batch Apex and verify that the Contacts Description should get updated with some value.
@isTestprivate class UpdateContactDescriptionTest {@testSetupstatic void dataSetup() {// Insert Contacts with some descriptionList<Account> listOfAccounts = new List<Account>();for(Integer i=0; i<10; i++) {listOfAccounts.add(new Account(Name = 'TestAcc'+i,Description = 'Account'+i + ' for Testing Batch Apex'));}insert listOfAccounts;// Contacts with some Account populatedList<Contact> listOfContacts = new List<Contact>();for(Account acc : listOfAccounts) {for(Integer j=0; j<5; j++) {listOfContacts.add(new Contact(LastName = 'TestAccCon'+j,AccountId = acc.Id,Title = 'Sales Executive'));}}// Contacts with no Account populatedfor(Integer k=0; k<20; k++) {listOfContacts.add(new Contact(LastName = 'NewCon'+k,Title = 'IT Engineer'));}insert listOfContacts;}@isTeststatic void testMethod_ContactsWithAccounts() {// Verify that description is empty for all the contacts before the batch class is executedList<Contact> contactsBeforeUpdation = [Select Id, Description From Contact];for(COntact con : contactsBeforeUpdation) {System.assert(con.Description == null, 'Description should not be empty');}Test.startTest();UpdateContactDescription batchClass = new UpdateContactDescription();Database.executeBatch(batchClass, 100);Test.stopTest();// Assert that description should be updated for Contacts with AccountList<Contact> contactsAfterUpdation_Acc = [Select Id, Description, Account.Name, Title From Contact Where AccountId != null];for(Contact con : contactsAfterUpdation_Acc) {System.assert(con.Description != null, 'Description should not be empty');System.assert(con.Description.contains(con.Account.Name), 'Description should contain Account Name');System.assert(con.Description.contains(con.Title), 'Description should contain Contact Title');}// Assert that description should not be updated and remain empty for Contacts with no AccountList<Contact> contactsAfterUpdation_noAcc = [Select Id, Description, Account.Name From Contact Where AccountId = null];for(Contact con : contactsAfterUpdation_noAcc) {System.assert(con.Description == null, 'Description should not be empty');}}}
Code Coverage: 100%