Thursday, 27 February 2014

How to write Test Class for a Batch Apex Class?


In continuation to my previous post on Batch Apex, here we will try to understand how to write test class for a batch apex class.
So let's have a sneak peek into it. 

Apex Class

global class AccountUpdate implements Database.Batchable {
     String query, field, value;
     global AccountUpdate(String f, String v){
          field = f;
          value = v;
          query = 'SELECT' + field + 'FROM Account';
     }
     global Database.QueryLocator start(Database.BatchableContext BC){
          return Database.getQueryLocator(query);
     }
     global void execute(Database.BatchableContext BC, List scope){
          for(sObject s: scope){
               s.put(field, value);
          }
     update scope;
     }
     global void finish(Database.BatchableContext BC){
     //
     }
}


Test class for above Batch Class


@isTest
public class AccountUpdateTest {
    static testMethod void test() {
        // Create an instance of query locator
        Database.QueryLocator QL;
        // Create an instance of batchable context
        Database.BatchableContext BC;
        List AcctList = new List();
        AccountUpdate oAccountUpdate = new AccountUpdate('Name', 'Test');
        // execute start method
        QL = oAccountUpdate.start(bc);       
        // Create an instance of Query Locator Iterator
        Database.QueryLocatorIterator QLI =  QL.iterator();
        while (QLI.hasNext())
        {
            Account Acc = (Account)QLI.next();        
            System.debug(Acc);
            AcctList.add(Acc);
        }               
        // initiate execute method
        oAccountUpdate.execute(BC, AcctList);
        // execute finish method
        oAccountUpdate.finish(BC);    
    }
}

6 comments:

  1. very useful...... see this link to get http://www.salesforcetraining.in/ for more basics for salesforce

    ReplyDelete
  2. This is definitely one of the best articles I have read in this website! Thanks Mate.

    Salesforce Course in Chennai

    ReplyDelete
  3. Thanks for sharing these niche piece of coding to our knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…
    Salesforce course in Chennai | Salesforce institutes in Chennai

    ReplyDelete
  4. Thanks for sharing this valuable information to our vision...

    Salesforce training in Chennai

    ReplyDelete
  5. The information you posted here is useful to make my career better. Thanks for sharing such a informative post. keep updates...

    Regards..
    Salesforce Administrator Training in Chennai

    ReplyDelete
  6. Cloud is one of the tremendous technology that any company in this world would rely on(cloud computing training in chennai). Using this technology many tough tasks can be accomplished easily in no time. Your content are also explaining the same(Best Institute for Cloud Computing in Chennai). Thanks for sharing this in here. You are running a great blog, keep up this good work.

    ReplyDelete

Thank you for visiting. Your comments are highly appreciated.