You need to sign in to do that
Don't have an account?
How to write a testclass for batch apex ?
Hi,
I am write a test class for batch apex,my code coverage is 33%, it is covered only start method.
How can i write test class for execute method?
Testclass for Batch Class
@isTest
private class testtrainingbatchcls {
static testmethod void m1() {
List<training__c> lst=new List<training__c>();
for(integer i=0;i<1500;i++) {
training__c t=new training__c();
t.name = 'T'+i;
lst.add(t);
}
trainingbatchcls obj=new trainingbatchcls();
Database.executeBatch(obj);
}
}
Batch Apex Class
global class trainingbatchcls implements Database.batchable <sobject>{
global list<training__c> start(Database.BatchableContext bc){
List<training__c> lst=new List<training__c>();
for(integer i=0;i<1500;i++){
training__c t=new training__c();
t.name = 'T'+i;
lst.add(t);
}
return lst;
}
global void execute(Database.BatchableContext bc,list<training__C>lst) {
insert lst;
}
global void finish(Database.BatchableContext bc){
list<string> toaddress = new list<string>();
toaddress.add('shravankumarbagam@yahoo.com');
String subject = 'Batch class run complete';
String body = '';
Messaging.singleEmailMessage mail = new Messaging.singleEmailMessage();
mail.setToAddresses(toaddress);
mail.setSenderDisplayName('Salesforce Outbound Notification');
mail.setSubject(subject);
mail.setPlainTextBody(body);
mail.setUseSignature(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
hi,
Now code coverage is 100% can you explain me 5,7,8,9 lines..
1. @isTest
2Private class testtrainingbatchcls{
3static testmethod void m1() {
4List<training__c> st=new List<training__c>();
5Database.BatchableContext bc;
6trainingbatchcls obj=new trainingbatchcls();
7st=obj.start(bc);
8obj.execute(bc,st);
9obj.finish(bc);
All Answers
Hi
Try this
@isTest
Private class testtrainingbatchcls{
static testmethod void m1() {
List<training__c> st=new List<training__c>();
Database.BatchableContext bc;
trainingbatchcls obj=new trainingbatchcls();
st=obj.start(bc);
obj.execute(bc,st);
obj.finish(bc);
}}
Regards,
Rajesh.
hi,
Now code coverage is 100% can you explain me 5,7,8,9 lines..
1. @isTest
2Private class testtrainingbatchcls{
3static testmethod void m1() {
4List<training__c> st=new List<training__c>();
5Database.BatchableContext bc;
6trainingbatchcls obj=new trainingbatchcls();
7st=obj.start(bc);
8obj.execute(bc,st);
9obj.finish(bc);
Hi,
I appreciated if any one can help me for writing the test class for below Batch class ..Thanks in Advance.
Here is the code:
global class UpdateClientTickerClass implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC){
system.debug(':- Inside Start');
//query = 'SELECT Id, DUNS_Number__c from Account WHERE ClientTickerFlag__c = true';
return Database.getQueryLocator('SELECT Id, DUNS_Number__c from Account WHERE ClientTickerFlag__c = true');
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
system.debug(':- Execute Started');
//List<Account> UpdatedAccounts = new List<Account>();
List<Client_Ticker__c> NewClientTickers= new List<Client_Ticker__c>();
List<Client_Ticker__c> DeleteClientTickers= new List<Client_Ticker__c>();
List<Account> clientToUpdate = new List<Account>();
for(Account S: Scope)
{
s.ClientTickerFlag__c = false;
S.Tax_ID__c = NULL;
system.debug(':- Inside Scope');
system.debug(':- Processing Client: '+ S.Id);
// Delete existing Client Tickers for the Accounts
for(Client_Ticker__c OCT :[select Id From Client_Ticker__c where Client__c =:s.id LIMIT 50000])
{
DeleteClientTickers.add(OCT);
system.debug(':- Ticker added to delete: '+ S.Id + ' : ' + OCT.id);
}
system.debug(':- Done with ticker deletion for client: '+ s.id);
if(S.DUNS_Number__c <> NULL && S.DUNS_Number__c <> '')
{
system.debug(':- DUNS not null for ' + S.Id);
// Lookup and Add New Client Tickers
FEIN__c[] Ultimate_Parent = [select Global_Ultimate_Parent_DUNS_Number__c, Name From FEIN__c where DUNS_Number__c = :String.valueOf(S.DUNS_Number__c) Limit 1];
Master_Ticker_List__c [] MTicker = [select Id, DUNS_Number__C, Ticker_Unique_Name__C From Master_Ticker_List__c where Ticker_Unique_Name__C like :S.DUNS_Number__c+'%' LIMIT 50000];
system.debug(':- Master tickers found for Client: '+ S.id + ' : '+ MTicker.size());
if(MTicker.size() > 0)
{
system.debug(':- Processing with CLient Duns number');
for(Master_Ticker_List__c MTicker_Rec: MTicker)
{
system.debug(':- Processing Master Ticker: ' + MTicker_rec.Id);
Client_Ticker__c CTicker_rec = new Client_Ticker__c();
CTicker_rec.Client__c = S.Id;
CTicker_rec.Master_Ticker_Number__c = MTicker_rec.ID;
NewClientTickers.add(CTicker_rec);
}
} else
{
system.debug(':- Processing with Ultimate Parent Duns number');
system.debug(':- Size of Ultimate Parent: '+ Ultimate_Parent.size());
if(Ultimate_Parent.size() > 0)
{
system.debug(':- Duns Number of Ultimate Parent: '+ Ultimate_Parent[0].Global_Ultimate_Parent_DUNS_Number__c);
MTicker = [select Id, DUNS_Number__C, Ticker_Unique_Name__C From Master_Ticker_List__c where Ticker_Unique_Name__C like :Ultimate_Parent[0].Global_Ultimate_Parent_DUNS_Number__c +'%' LIMIT 50000];
system.debug(':- Master tickers found for Ultimate Parent : '+ S.id + ' : '+ MTicker.size());
for(Master_Ticker_List__c MTicker_Rec: MTicker)
{
system.debug(':- Processing Master Ticker from ultimate parent: ' + MTicker_rec.Id);
Client_Ticker__c CTicker_rec = new Client_Ticker__c();
CTicker_rec.Client__c = S.Id;
CTicker_rec.Master_Ticker_Number__c = MTicker_rec.ID;
NewClientTickers.add(CTicker_rec);
}
}
}
//Update FEIN for client
if(Ultimate_Parent.size() > 0)
{
s.Tax_ID__c = Ultimate_Parent[0].Name;
}
//FEIN update done
}
system.debug(':- Finished processing Client: ' + s.Id);
clientToUpdate.add(s);
}// End of for loop
if(NewClientTickers.size() > 0)
{
insert NewClientTickers;
}
if(DeleteClientTickers.size() > 0)
{
delete DeleteClientTickers;
}
if(clientToUpdate.size() > 0)
{
update clientToUpdate;
}
}
global void finish(Database.BatchableContext BC){
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email FROM AsyncApexJob WHERE Id = :BC.getJobId()];
// Send an email to the Apex job's submitter
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Client Ticker Status: ' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}