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

write test class for batch
Hi,
I write a batch class for my project; i want to test: plese help me to write in test class in order to covered my class.
Thanks.
this my batch class
global class BalanceCalculBatch implements Database.Batchable<sObject>{
String IDVar;
decimal var1=0, var2=0;
date dateEcheance;
String email;
global Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator([SELECT id,name FROM Opportunity]);}
global void execute(Database.BatchableContext BC, List<Sobject> scope){
List<Opportunity> opps = [select id from Opportunity where Date_d_ech_ance_de_la_police__c > 2011-12-31];
Integer l;
l = opps.size();
if(l!=Null) {
for (Integer i = 0; i<l; i++)
{
IDVar=opps[i].Id;
/******************************************************************************************************************************/
List<paiement__c> paiement = [SELECT Id, montant_pay__c ,Encaissement_remboursement__c, Montant_rem__c,
Opportunit__c FROM paiement__c where Opportunit__c=:IDVar];
Integer m;
m = paiement.size();
if(m!=Null) {
for (Integer s = 0; s<m; s++)
{
if(paiement[s].Encaissement_remboursement__c=='Encaissement')
{
var1 += paiement[s].montant_pay__c;
}
if(paiement[s].Encaissement_remboursement__c=='Remboursement')
{
var1 -= paiement[s].Montant_rem__c;
}
}
}
/******************************************************************************************************************************/
dateEcheance=opps[i].Date_d_ech_ance_de_la_police__c;
List<Quittance__c> quittance = [SELECT Id, Date_due1__c, Echeance_paiement__c, Echeance1__c,
Opportunit__c, Facturation_Avoir__c, Montant_d_avoir__c
FROM Quittance__c where Opportunit__c =:IDVar];
/******************************************************************************************************************************/
Integer h;
h = quittance.size();
if(h!=null)
{
for (Integer n = 0; n<h; n++)
{
if(quittance[n].Facturation_Avoir__c=='Avoir')
{
var2 -= quittance[n].Montant_d_avoir__c;
}
if(quittance[n].Facturation_Avoir__c=='Facturation')
{
/****************************************************************************************************/
if (quittance[n].Echeance_paiement__c =='Annuel' && quittance[n].Date_due1__c<=datetime.now())
{
var2+=quittance[n].Echeance1__c;
}
}
}
}
opps[i].Total_montant_pay__c = var1-var2;
/****************************************************************************************************************/
update opps[i];
var1=0;
var2=0;
}
}
}
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 notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Sharing Recalculation ' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
you just need to call that batch in your test class.
Database.executeBatch(new BatchName({INSERT QEURY HERE});
it may also help to set up some simple test data and use that as you query data because even if you call a batch in a test class it will still try to convert your real data.
if this is a one time batch you can also deploy it with no test class (NOTE: this is not best practice) and remove it from prod after. as lone as you have enough total global test coverage (75%).
If this is something that you need to fire often i would conside making this a bulkified class instead of a batch. this can be done by moving all query's out of any loops so they are only called once.