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
Navya sree 4Navya sree 4 

Test Class Help for Batch class?

Hi ,
Can you please help me with test class


global class UpdateUnitHighestStageBatchJob implements Database.Batchable<sObject>{
    
    private List<Id> unitIds;

    public UpdateUnitHighestStageBatchJob() {

    }

    public UpdateUnitHighestStageBatchJob(List<Id> parameter) {
        unitIds = parameter;
    }

    global Database.QueryLocator start(Database.BatchableContext context) {
        String soql = 'SELECT Id, Opportunity_Highest_Stage__c FROM Unit__c';
        if (null != unitIds && !unitIds.isEmpty()) {
            soql += ' WHERE Id IN :unitIds';
        }
        return Database.getQueryLocator(soql);
    }
    
    global void execute (Database.BatchableContext context , List<sObject> scope) {
        List<Unit__c> units = (List<Unit__c>) scope;
        Map<Id, Unit__c> units2Update = new Map<Id, Unit__c>();
        for (Unit__c unit: units) {
            List<Unit_Opportunity_Relationship__c> uors = [SELECT Unit__c, Opportunity__r.StageName FROM Unit_Opportunity_Relationship__c 
                        WHERE Unit__c = :unit.Id AND Opportunity__r.StageName != 'Closed Won' AND 
                            Opportunity__r.StageName != 'Closed Lost'];
            String OpportunityHighestStage = 'Z';
            for (Unit_Opportunity_Relationship__c uor : uors) {
                if (OpportunityHighestStage.codePointAt(0) > uor.Opportunity__r.StageName.codePointAt(0)) {
                    OpportunityHighestStage = uor.Opportunity__r.StageName;
                }
              }
              if ('Z' != OpportunityHighestStage) {
                unit.Opportunity_Highest_Stage__c = OpportunityHighestStage;
                units2Update.put(unit.Id, unit);
            }        
        }
        
        if (!units2Update.isEmpty()) {
            update(units2Update.values());    
        }
    }

    global void finish (Database.BatchableContext context) {

    }
}
Raj VakatiRaj Vakati
Try this cod e.. 


ADD ALL required fields to object 
 
@isTest
public class UpdateUnitHighestStageBatchJobTest {
@isTest static void testBatchMethid(){
	
	
	  Account a = new Account();
        a.Name = 'Test';
        
        a.Industry = 'Retail';
        
        insert a;
        
        
        Opportunity o = new Opportunity();
        o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Prospecting';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;
		
		
	list<Unit__c> uni=new list<Unit__c>();
	for(integer i=0;i<10;i++){
		Unit__c g=new Unit__c();
		g.Name='ABSYZ';
		g.Opportunity_Highest_Stage__c ='Prospecting';
		g.Opportunity__c = o.Id ;
		// Add other required filed
		uni.add(g);
	}
	insert uni;
	
	Unit_Opportunity_Relationship__c or11 = new Unit_Opportunity_Relationship__c(0 ;
	or11.Unit__c = uni[0].Id ; 
		or11.Opportunity__c = o.Id ; 
		insert or11 ; 
		Unit_Opportunity_Relationship__c or12 = new Unit_Opportunity_Relationship__c(0 ;
	or12.Unit__c = uni[1].Id ; 
		or12.Opportunity__c = o.Id ; 
		insert or12 ; 
	   
Test.startTest();
Map<Id,Unit__c> amps = new Map<Id,Unit__c>([select Id from Unit__c]);

List<Id> ids = new List<Id>();
ids.addall(amps.keySet());
  UpdateUnitHighestStageBatchJob  x = new UpdateUnitHighestStageBatchJob (ids);
database.executeBatch(x);
Test.stopTest();
}
}