You need to sign in to do that
Don't have an account?
SHAIK MOHAMMAD YASEEN
test class for batch class, need to cover else part if batch has failed in finish method
Hi All,
I am having code below. for a batch, when ever my batch fails i will send an email to admin with the current batch status, can you please help me how to cover the else part in the finish method.
public class AccountBatch implements Database.Batchable<sObject>
{
public database.QueryLocator start(Database.BatchableContext bc)
{
string Query='select id, Name From Account';
system.debug('Query-->'+Query);
return database.getQueryLocator(Query);
}
public void execute(database.BatchableContext bc,list<sObject> scope)
{
helperClass test = new helperClass();
test.helperMethod(scope);
}
public void finish(Database.BatchableContext bc)
{
AsyncApexJob a = [Select Id, Status, NumberofErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email, ExtendedStatus
from AsyncApexJob
Where Id =:bc.getJobId()];
String batchName = 'AccountBatch';
If(a.Status=='Completed' && a.NumberofErrors==0)
{
Database.executeBatch(new nextBatch(),50);
}
else
{
System.debug('invalid batch');
UtilityCls bu = new UtilityCls();
bu.sendEmailNotificationOnBatchJobFail(batchName, a.Status, a.TotalJobItems, a.NumberOfErrors,a.ExtendedStatus);
}
}
}
I am having code below. for a batch, when ever my batch fails i will send an email to admin with the current batch status, can you please help me how to cover the else part in the finish method.
public class AccountBatch implements Database.Batchable<sObject>
{
public database.QueryLocator start(Database.BatchableContext bc)
{
string Query='select id, Name From Account';
system.debug('Query-->'+Query);
return database.getQueryLocator(Query);
}
public void execute(database.BatchableContext bc,list<sObject> scope)
{
helperClass test = new helperClass();
test.helperMethod(scope);
}
public void finish(Database.BatchableContext bc)
{
AsyncApexJob a = [Select Id, Status, NumberofErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email, ExtendedStatus
from AsyncApexJob
Where Id =:bc.getJobId()];
String batchName = 'AccountBatch';
If(a.Status=='Completed' && a.NumberofErrors==0)
{
Database.executeBatch(new nextBatch(),50);
}
else
{
System.debug('invalid batch');
UtilityCls bu = new UtilityCls();
bu.sendEmailNotificationOnBatchJobFail(batchName, a.Status, a.TotalJobItems, a.NumberOfErrors,a.ExtendedStatus);
}
}
}
Raj Vakati
Try this