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
goodankit@yahoo.comgoodankit@yahoo.com 

how to write in test class batch Batch Apex method

Subhani PSubhani P

Hi,

 

Please check the following data.

 

Sample Code
Batch Class :
1: global class ExampleBatchClass implements Database.Batchable<sObject>{
2:
3: global ExampleBatchClass(){
4: // Batch Constructor
5: }
6:
7: // Start Method
8: global Database.QueryLocator start(Database.BatchableContext BC){
9: return Database.getQueryLocator(query);
10: }
11:
12: // Execute Logic
13: global void execute(Database.BatchableContext BC, List<sObject> scope){
14: // Logic to be Executed batch wise
15:
16: }
17:
18: global void finish(Database.BatchableContext BC){
19: // Logic to be Executed at finish
20: }
21: }

 

Call the Batch Class :

1: ExampleBatchClass b = new ExampleBatchClass();
2: //Parameters of ExecuteBatch(context,BatchSize)
3: database.executebatch(b,10);

 

Note : if batch size is not mentioned it is 200 by default.

That’s about it for this post, if you still want a deep dive into Batch Classes I suggest you read this

Do let me know your thoughts Happy Coding !

 

For more reference check the following link.

 

http://blog.shivanathd.com/2013/01/how-to-write-batch-class-in.html

 

Thanks,

Subhani,

DBSync,

www.mydbsync.com

Subhani PSubhani P

Hi

 

I have found out same post in developer's community.

 

Check the following link

 

http://boards.developerforce.com/t5/Apex-Code-Development/how-do-i-write-test-class-for-batch-apex-scheduler/td-p/507505

 

 

Thanks & Regards,

Subhani,

DBSync,

www.mydbsync.com

souvik9086souvik9086

Do like this

 

In Test Class

YourObject obj = new YourObject();

insert obj;

List<YourObject> objList = new List<YourObject>();

RecordBatchApexClassName recBatchApex = new RecordBatchApexClassName();   

objList.add(obj);
recBatchApex.listAuditRecords .clear();
Database.BatchableContext dbc;
recBatchApex.start(dbc);
recBatchApex.execute(dbc,objList);
recBatchApex.finish(dbc);

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks