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
Narasimha ReddyNarasimha Reddy 

How to write test class this Batch class

Global Class WL_ValidationProcess1Batch implements Database.Batchable<SObject>{
    String usrId = 'xxxxxxxxxxxxxxxxxx';
    global final String Query = 'select id, Account__c, Contract__c, Contract_Area__c, Customer_Type__c, StartDate__c from WL_CustomerJourney__c ' +
                                        'where Status__c =\''+System.Label.WL_Picklist_Active +'\'' + ' and OwnerId !=\''+ System.Label.WL_WorkingLinkQueue +'\'' +
                                         'and CreatedByid =\''+usrId+'\'';
       
    /*global WL_ValidationProcess2Batch(String strQuery){
        Query = strQuery;
    }*/
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        system.debug('==============='+Query);
        return Database.getQueryLocator(Query);
    }

    global void execute(Database.BatchableContext BC, List<WL_CustomerJourney__c> scope){
        Set<Id> custIds = (new Map<Id, WL_CustomerJourney__c>(scope)).keySet();
        List<WL_SubContractorPayment__c> insertSubContractorPayemnt = new List<WL_SubContractorPayment__c>();
        Map<id, WL_CustomerAchievement__c> mapCustAhievement = new Map<id, WL_CustomerAchievement__c>();
        List<WL_Validation__c> lstUpdateValidation =  new List<WL_Validation__c>();
        List<WL_Validation__c> lstValidations = [select Customer_Journey__c, Job_ID__c, Customer__c, Customer_Qualification__c,
                                                 Job_ID__r.Organisation__c, Job_ID__r.EmploymentStartDate__c,Customer_Journey__r.StartDate__c,
                                                 Customer_Journey__r.Completer_date__c,Customer_Journey_Start_date__c, Customer_Qualification__r.Qualification__c,
                                                 Customer_Qualification__r.Validated__c, Customer_Qualification__r.Qualification__r.Qualification__c
                                                 from  WL_Validation__c where Validation_Candidate__c=false and
                                                 Customer_Journey__c !=: null and Customer_Journey__c in: custIds];
                                                 
        List<WL_Milestone__c> lstMilestones = new List<WL_Milestone__c>([select id, Name, Milestone_Type__c, Validation_Required__c,                        
                                                        Contract__c, Funder_Milestone__c, Sub_contractor_Milestone__c from WL_Milestone__c]);                                        
        
        Map<String, WL_Milestone__c> mapMilestones = new Map<String, WL_Milestone__c> ();

        for(WL_Milestone__c milestone : lstMilestones){
            mapMilestones.put(milestone.Name, milestone);
        }       
        System.debug('****List Validations='+lstValidations);
        
        Set<Id> accIds = new Set<Id>();
        
        for(WL_CustomerJourney__c custJourney : scope){
            for(WL_Validation__c validation : lstValidations){
                if(validation.Job_ID__r.Organisation__c != null)
                    accIds.add(validation.Job_ID__r.Organisation__c);       
            }
        }
        
        // get contact based on account
        Set<Contact> setContact = new Set<Contact>();
        setContact.addAll((new Map<Id, Contact>([select id, AccountId from contact where AccountId in : accIds])).Values());
        Map<Id, Integer> mapContactCount = new Map<Id, Integer>();
        
        // get contac count for each account
        for(Id accId : accIds){
            Integer count = 0;
            for(Contact contact : setContact){
                if(accId == contact.AccountId)
                    count += 1;
            }
            mapContactCount.put(accId, count);
        }
           
        for(WL_Validation__c validation : lstValidations){
            //  is attachment milestone achieved
            system.debug('===============27'+validation);
            if(WL_MilestonesUtils.isAttachementMilestone(validation)){
                System.debug('===inside if****'+validation);
                WL_MilestonesUtils.processValidations(validation, true, lstUpdateValidation, insertSubContractorPayemnt, 
                mapCustAhievement, mapMilestones.get(System.Label.WL_milestone_Attachment), null, null, null);
            }
            //  is Qualification milestone achieved
            if(WL_MilestonesUtils.isQualificationMilestone(validation)){
                WL_MilestonesUtils.processValidations(validation, true, lstUpdateValidation, insertSubContractorPayemnt, 
                mapCustAhievement, mapMilestones.get(System.Label.WL_milestone_Qualification), null, null, null);
            }
            //  is Sub-Contractor milestone achieved
            if(WL_MilestonesUtils.isSubContractorMilestone(validation, mapContactCount)){
                WL_MilestonesUtils.processValidations(validation, true, lstUpdateValidation, insertSubContractorPayemnt, 
                mapCustAhievement, mapMilestones.get(System.Label.WL_milestone_SubContractor), null, null, null);
            }
            // get the no of days for customer to worked for a organization
            if(WL_MilestonesUtils.isValidJob(validation, mapContactCount)){
                WL_MilestonesUtils.processValidations(validation, true, lstUpdateValidation, insertSubContractorPayemnt, 
                mapCustAhievement, mapMilestones.get(System.Label.WL_milestone_13WeeksInJob), null, null, null);
            }
        }
        
        // set validation candidate flag to true on Validation object records from First batch execution.
        //update Customer achievement ID from newly created customer achievement on Validation object records from Second batch execution.
        if(lstUpdateValidation.size() >=1) {
            System.debug('===============27'+lstValidations);
            Set<WL_Validation__c> validationSet = new Set<WL_Validation__c>();
            List<WL_Validation__c> validationList=new List<WL_Validation__c>();
            validationSet.addAll(lstUpdateValidation);
            validationList.addAll(validationSet);
            update validationList;
        }    
    }
    //finish method of batch
    global void finish(Database.BatchableContext bc){
    // write logic which required after completing batch
    }   
}
Sanjay.GeorgeSanjay.George
Seems like you have hardcoded a User ID in the batch. (It's a bad practise.)

Keep the User ID field as a static variable and before calling the batch class assign it the class, else you may provide a default value. This will ensure your class runs.

While Writting test class, make sure to create records inside 'System.runas(user)' and assign the user ID to the batch class before calling it.
 
Global Class WL_ValidationProcess1Batch implements Database.Batchable<SObject>{
    public static String usrId ;
  
    global Database.QueryLocator start(Database.BatchableContext BC){
                if(usrId==null){
                 usrId = 'xxxxxxxxxxxxxxxxxx';
                }

        return Database.getQueryLocator(Query);
    }
 
/*
Test Class
*/

WL_ValidationProcess1Batch.userid = 'TestUserID';
WL_ValidationProcess1Batch bint = new WL_ValidationProcess1Batch ();
database.executeBatch(bint);