+ Start a Discussion
rajubalajirajubalaji 

how to write a test class for batch apex?

Hello Devs,
    Not sure what i have to modify in btach test class to make it 75% coverage , but right now it's only giving me 7%, any help is appreciated.please help me.

public class PendingUserTaskCreation implements Database.Batchable<Account>, Database.AllowsCallouts {
    public PendingUserTaskCreation() {
        
    }
    
    // Start Method
    public Iterable<Account> start(Database.BatchableContext BC){
        List<Account> lstAccount = [Select Id, Name, starId__c, Enviornment_Code__c from Account Where Pending_User_Task__c = true];
        return lstAccount;
    }
    
    // Execute Logic
    public void execute(Database.BatchableContext BC, List<Account> lstAccount){
        try {
            Datetime registrationStartDay = Datetime.now().date().addDays(-Integer.valueOf(Label.Registration_Task_Pending_Days));
            Date duedate = Utility.addBusinessdays(0);
            Integer count = 1;
            List<User> lstUser = Utility.GetUserListForTaskAssignment(lstAccount.get(0).Id, Label.Registration_Task);
            string recordTypeId = Utility.getRecordTypeID(Label.Registration_Task);
            List<Task> lstTasks = new List<Task>();
            for (Hub_Parent__c pt : [Select Id, AccountId__c, Registration_Completed_Date__c, Star_Display__c, (Select Id, Status, Type, CreatedDate from Tasks where Type =: Label.Registration_Task Order By CreatedDate desc Limit 1) from Hub_Parent__c Where AccountID__c = :lstAccount.get(0).Id and Product_Status__c = 'LoginPending' and Registration_Completed_Date__c <: registrationStartDay]) {
                if (pt.Tasks.size() == 0) {
                    Task objTask = new Task();
                    objTask.Type = 'Registration Task';
                    objTask.Status = 'Open';
                    objTask.Priority = 'High';
                    objTask.OwnerId = lstUser[(Integer)math.mod(count, lstUser.size())].Id;
                    count = count + 1;
                    objTask.WhatId = pt.Id;
                    objTask.Parents__c = pt.Id;
                    objTask.Account__c = pt.AccountId__c;
                    objTask.ActivityDate = duedate;
                    objTask.Description__c = Label.Registration_Task_Description;
                    objTask.IsVisibleInSelfService = true;
                    objTask.RecordTypeId = recordTypeId;
                    lstTasks.add(objTask);
                }
            }
            if (lstTasks.size() > 0) {
                List<Database.SaveResult> lstResult = Database.insert(lstTasks, false);
                String message = '';
                for (Database.SaveResult result:lstResult) {
                    if (!result.isSuccess()) {
                        Database.Error[] errs = result.getErrors();
                        for(Integer i=0;i<errs.size();i++){
                            message = message + '\\n' + errs[i].getMessage(); 
                        }
                    }
                }
                if (message != '') {
                    System.debug('PendingUserTaskCreation Error : ' + message);
                }
            }
        }
        catch (Exception ex) {
            System.debug('PendingUserTaskCreation Exception for Account - ' + lstAccount.get(0).Id + ' : ' + ex.getMessage());
        }
    }
    
    // Logic to be Executed at finish
    public void finish(Database.BatchableContext BC){
        
    }
}

Thanks Inadvance,
RajuBalaji.

Please help me with explaination if possible.
AnudeepAnudeep (Salesforce Developers) 
See this sample test class code for batch apex