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

write test class for batch



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.




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++)
          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++)
                                var1 += paiement[s].montant_pay__c;
                                var1 -= paiement[s].Montant_rem__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();     
     for (Integer n = 0; n<h; n++)
               var2 -= quittance[n].Montant_d_avoir__c;

               if (quittance[n].Echeance_paiement__c =='Annuel' && quittance[n].Date_due1__c<

opps[i].Total_montant_pay__c = var1-var2;  
                  update opps[i];
global void finish(Database.BatchableContext BC){
   AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
      TotalJobItems, CreatedBy.Email
      FROM AsyncApexJob WHERE Id =
   // 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.setSubject('Apex Sharing Recalculation ' + a.Status);
   ('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.