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);
}
}
very useful...... see this link to get http://www.salesforcetraining.in/ for more basics for salesforce
ReplyDeleteThis is definitely one of the best articles I have read in this website! Thanks Mate.
ReplyDeleteSalesforce Course in Chennai
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…
ReplyDeleteSalesforce course in Chennai | Salesforce institutes in Chennai
Thanks for sharing this valuable information to our vision...
ReplyDeleteSalesforce training in Chennai
The information you posted here is useful to make my career better. Thanks for sharing such a informative post. keep updates...
ReplyDeleteRegards..
Salesforce Administrator Training in Chennai
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