You need to sign in to do that
Don't have an account?

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) {
}
}
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) {
}
}
ADD ALL required fields to object