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
Nishad BashaNishad Basha 

How write the Test class for batch apex?

how write the Test class for batch apex we need for our requirement.ple
global class bchrankupdate implements database.batchable<sObject>{

 List<Batchrank__c> newbatchmark = new List<Batchrank__c>();
 
  global database.querylocator start(database.BatchableContext BC){
   String Query = 'SELECT name, Percents__c, TestRanks__c, Rank__c FROM Batchrank__c';
   return Database.getQueryLocator(query);
  }
  
  global void execute(database.BatchableContext BC, List<Batchrank__c> s){
  system.debug('batch size:'+s.size());
   for(Batchrank__c br : s){
   system.debug('record:'+br);
   
   if(br.Percents__c >= 90 && br.Percents__c <= 100){
    if(br.TestRanks__c=='First')   
   {
    br.Rank__c = '1';
     newbatchmark.add(br);
     
   }else{
     br.Rank__c = 'Empty';
     newbatchmark.add(br);
     }
          
   }else if(br.Percents__c >= 80 && br.Percents__c <= 89){
     if(br.TestRanks__c=='Second'){
     br.Rank__c = '2';
     newbatchmark.add(br);
     
   }else{
     br.Rank__c = ' ';
     newbatchmark.add(br);
     }
        
   }else if(br.Percents__c >= 70 && br.Percents__c <= 79){
     if(br.TestRanks__c=='Third'){
      br.Rank__c = '3';
      newbatchmark.add(br);
     
   }else{
     br.Rank__c = ' ';
     newbatchmark.add(br);
     }
            
   }else if(br.Percents__c >= 60 && br.Percents__c <= 69){
     if(br.TestRanks__c=='Fourth'){
     br.Rank__c = '4';
     newbatchmark.add(br);
     
   }else{
     br.Rank__c = ' ';
     newbatchmark.add(br);
     }
   }else if(br.Percents__c >= 40 && br.Percents__c <= 59){
     if(br.TestRanks__c=='Fifth'){
     br.Rank__c = '5';
     newbatchmark.add(br);
     
   }else{
     br.Rank__c = ' ';
     newbatchmark.add(br);
     }
   }
      
      update newbatchmark;
      
   }
  } 
  
  global void finish(database.batchablecontext BC){
  
  
  }
}

ase give some ideas.
Best Answer chosen by Nishad Basha
Shashikant SharmaShashikant Sharma
You could read this it has an example of test class for a Batch Apex class.

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_batch_2.htm

All Answers

Nishad BashaNishad Basha
how to write a test class for above scenario. please give some ideas.
Shashikant SharmaShashikant Sharma
You could read this it has an example of test class for a Batch Apex class.

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_batch_2.htm
This was selected as the best answer
Nishad BashaNishad Basha
Hi, Shashikant Sharma
 i need apex code  coverage for 75%.  for  below code .how can i achieve please give some ideas.
 
@isTest
public class bchrankupdateTest{
   String Query = 'SELECT name, Percents__c, TestRanks__c, Rank__c FROM Batchrank__c';
  List<Batchrank__c> newbatchmark = new List<Batchrank__c>();
  static testmethod void rankupdate(){
    
   // Test.StartTest();  
   
   
   
    List<Batchrank__c> newbatchmark = new List<Batchrank__c>();  
    Batchrank__c b = new Batchrank__c(name = 'test');
   
     newbatchmark.add(b);
     insert newbatchmark;
     
   if(b.Percents__c >= 90 && b.Percents__c <= 100){
    if(b.TestRanks__c=='First')   
   {
    b.Rank__c = '1';
     newbatchmark.add(b);
     
   }else{
     b.Rank__c = '';
     newbatchmark.add(b);
     }
          
   }else if(b.Percents__c >= 80 && b.Percents__c <= 89){
     if(b.TestRanks__c=='Second'){
     b.Rank__c = '2';
     newbatchmark.add(b);
     
   }else{
     b.Rank__c = ' ';
     newbatchmark.add(b);
     }
        
   }else if(b.Percents__c >= 70 && b.Percents__c <= 79){
     if(b.TestRanks__c=='Third'){
      b.Rank__c = '3';
      newbatchmark.add(b);
     
   }else{
     b.Rank__c = ' ';
     newbatchmark.add(b);
     }
            
   }else if(b.Percents__c >= 60 && b.Percents__c <= 69){
     if(b.TestRanks__c=='Fourth'){
     b.Rank__c = '4';
     newbatchmark.add(b);
     
   }else{
     b.Rank__c = ' ';
     newbatchmark.add(b);
     }
   }else if(b.Percents__c >= 40 && b.Percents__c <= 59){
     if(b.TestRanks__c=='Fifth'){
     b.Rank__c = '5';
     newbatchmark.add(b);
     
   }else{
     b.Rank__c = ' ';
     newbatchmark.add(b);
     }
   } 
      
    //  update newbatchmark;

     
   // Test.StopTest();
  
  }
}

 
Shashikant SharmaShashikant Sharma
Hi Nishad,

You have lots of conditions if else so you have to make sure that you create data to execute all these conditions. You could create multiple test methods with appropriate data for conditions so overall coverage by all methods will increase.

Thanks
Shashikant
Nishad BashaNishad Basha

i need apex code coverage for 75%. how can i achieve please give some ideas.
Nishad BashaNishad Basha

Hi, Shashikant Sharma

i need apex code  coverage for 75%. can  you  give me above code modification for example
Nishad BashaNishad Basha

Hi, Shashikant Sharma

 can  you  give me above code modification for example. apex code  coverage for 75%.
 
Nishad BashaNishad Basha

Hi, Shashikant Sharma

without using if condition else how to run the above test class  .please send me related code.