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
sumit dsumit d 

how to write a test class of this batch?

public class BatchAccountUpdate implements Database.Batchable<sObject>, Database.Stateful {
    
    public Database.QueryLocator start(Database.BatchableContext BC){
        String query =  ' Select Id, Name,ParentId'
            + ' from Account ' 
            + ' where Account.Recordtype.DeveloperName = \'IndustriesIndividual\' ' 
            + ' and ParentId = Null ';
        
        
        return Database.getQueryLocator(query);
    }
    
    
    public void execute(Database.BatchableContext bc, List<Account> accounts ){
        
        Map<Id, Account> accountToUpdateMap = new Map<Id, Account>();
        Map<Id, Id> mapAccountIndvAccIdToHouseholdAccId = new Map<Id, Id>();
        
        // fetch primary contact for individual account 
        // and inner query to fetch house hold account contact relation
        for(Contact con : [SELECT Id, AccountId,Account.ParentId,
                           ( SELECT Id, ContactId, AccountId
                            FROM AccountContactRelations 
                            WHERE Account.RecordType.DeveloperName = 'IndustriesHousehold' 
                           )
                           FROM Contact 
                           WHERE AccountId in: accounts 
                          ] ){
                              
                   for( AccountContactRelation acr : con.AccountContactRelations ) {
                         con.Account.ParentId = acr.AccountId;
                         mapAccountIndvAccIdToHouseholdAccId.put( con.AccountId, acr.AccountId );
                   }
        }
        
        for(Account acc : [ SELECT Id, ParentId 
                            FROM Account 
                            WHERE Id IN: accounts
                          ]){
                              
            if( acc.ParentId == Null && mapAccountIndvAccIdToHouseholdAccId.containsKey( acc.Id )){
                Account accToUpdate = new Account( Id = acc.Id, 
                                                   ParentId = mapAccountIndvAccIdToHouseholdAccId.get(  acc.Id ));
                accountToUpdateMap.put( accToUpdate.Id, accToUpdate );
            } 
        }
         
        if( accountToUpdateMap.size() > 0 ){
            update accountToUpdateMap.values();
        } 
    }
     
    public void finish(Database.BatchableContext bc){ 
        
    }
}
Leonardi KohLeonardi Koh
I recommend you read the topic in
https://trailhead.salesforce.com/en/modules/asynchronous_apex/units/async_apex_batch

under the Testing Batch Apex section.

But the basic gist is that you would prepare the test class as normal, and then execute the test class using
Database.executeBatch(*insert the name of the batch class that needs to be executed here*)