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
ponneri navadeepponneri navadeep 

how to write test class for batch apex?

Amit Chaudhary 8Amit Chaudhary 8
Please check below post for Batch  job related query
1) http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html

Sample Batch job
global class AccountUpdateBatchJob implements Database.Batchable<sObject> 
{
    global Database.QueryLocator start(Database.BatchableContext BC) 
    {
        String query = 'SELECT Id,Name FROM Account';
        
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Account> scope) 
    {
        for(Account a : scope)
        {
            a.Name = a.Name + 'Updated by Batch job';
        }
        update scope;
    }
    global void finish(Database.BatchableContext BC) {
    }
}
Sample test class.
@isTest 
public class AccountUpdateBatchJobTest 
{
    static testMethod void testMethod1() 
    {
        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <200;i++)
        {
            Account acc = new Account();
            acc.Name ='Name'+i;
            lstLead.add(acc);
        }
        
        insert lstAccount;
        
        Test.startTest();

            AccountUpdateBatchJob obj = new AccountUpdateBatchJob();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
    }
}

Let us know if this will help you

Thanks
Amit Chaudhary
 
nandu s 15nandu s 15
Superb explanation thanks....
 
Lucas MarianoLucas Mariano
The test class is correct, but you are not testing anything here, it's just a matter of code coverage.

You should use
System.assert(yourExpectedResult);
to ensure the code is actually working as expected.
Amit Chaudhary 8Amit Chaudhary 8
Yes i agree. Please follow below best pratice for test classes
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Please follow below salesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required 
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .


Please let us know if this post will help you
Jackson SegalaJackson Segala

Hi Amit Chaudhary 8,

I did what you told to, but when i try to use the Assert, the time in debuglog of the assert, happens before than the batch finishes, how should i proceed? there is a way to wait the AsyncApexJob to finish?
 

Abhirup MukherjeeAbhirup Mukherjee
You can try this out: 
Worked perfectly for me! 
Apex Class: 
global class LeadProcessor implements database.Batchable<sObject>{
    global database.querylocator start(database.batchablecontext bc){
       string query='select id,name,leadsource from lead order by name';
     return database.getquerylocator(query);   
    }
    global void execute(database.BatchableContext bc,list<lead> a){
        for(lead l:a)
        {
       l.leadsource='Dreamforce';            
        }
}
    global void finish(database.batchablecontext bc){
        system.debug('records inserted');
    }
}

Test Class: 
@isTest
public class LeadProcessorTest {
    @isTest static void check(){
        list<lead>c=new list<lead>();
        for(integer i=0;i<200;i++){
            lead g=new lead();
            g.LastName='ABSYZ';
            g.Company='ABSYZ';
            c.add(g);
        }
        insert c;
           
  Test.startTest();
      leadprocessor x = new leadprocessor();
    database.executeBatch(x);
    Test.stopTest();
    }
}
Hope, this helps! Thanks.
 
Evan Arbeitman 16Evan Arbeitman 16
Hi,

I'm looking to do something similar. I'm having an issue invoking the finish method in my test class. Were you able to get the debug log to print in this example?
Jackson SegalaJackson Segala
i just stopped working with SF, that was my workaround (:
Evan Arbeitman 16Evan Arbeitman 16
I envy you haha. But for now, I'd like to get this working.
Abhirup MukherjeeAbhirup Mukherjee
Hi Evan!
For me, the debug log was processed and I got the log output as specified "records inserted".
Surender reddy SalukutiSurender reddy Salukuti
Hi sir,
when we are execyting batch apex from Debug (open Execute annanymous window) at that time we are excuting like
Creating object for class 
                                         LeadProcessor   l = new LeadProcessor();
                                        id jobid =Database.Executebatch(l,5);

      hear we are giving batch size but when we are using Testclasses in the above didn't mentioned any batch size and how can we give batch size
  if we given batch size how can i write for loop logic

Thank you
Surender
 
Rasuhan KumarRasuhan Kumar
Hi Surender,
In test class also you can adjust the batch size in the similiar way as  we do it in the Anonymous window.
test.starttest();
 LeadProcessor   l = new LeadProcessor();
 Database.Executebatch(l,5);
test.stoptest();
Hope this answers your question.
Thanks,
Raushan Kumar
Ajay K DubediAjay K Dubedi
Hi,

In Order to write a test class for batch class. 

Please follow below steps for best Practices:

1. Use @isTest at the Top for all the test classes
2. Always put assert statements for negative and positive tests
3. Use the @testSetup method to insert the test data into the Test class that will flow all over the test class.
4. Always use Test.startTest() and Test.stopTest() doing this it will increase the governor limit of the salesforce. We also use this to increase the governor's limit.
5. Use System.runAs() method to test the functionality in user Context.
6. Do not put (seeAllData = true) in test class otherwise, use it for exceptional cases.
7. Avoid Using Hard Coding Ids anywhere in test Class or any apex class.
8. Please make sure that each class has a minimum of 75% coverage and also the main functionality has been covered. If possible increase code coverage up to 95%.
9. All class methods must be tested for at least 200 records and keep the real scenarios in mind.
10.Only one Test.startTest() and Test.stopTest() statement can be in a method, and no of Test.startTest() and Test.stopTest() statement in any test class depend upon the test methods.

Batch Class:
 
global class Test_Class implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,abc__c,CloseDate,StageName,Name FROM Opportunity';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext bc, List<opportunity> scope)
    {
        List<opportunity>  ls=new list<opportunity>();
        Map<Id,opportunity> update_Map = new Map<Id,opportunity>();
        for(opportunity oppty:scope)
        {
            if(oppty.abc__c=='P' && oppty.abc__c!='M' 
               && oppty.createdby.profile.Name=='Custom: TCT')
            {
                oppty.abc__c='M';
                //Updated below opptyListUpdate List to update_Map Map as to avoid duplicate values
                update_Map.put(oppty.Id,oppty);
                ls.add(oppty);
                break;
            }
        }
    }
    global void finish(Database.BatchableContext BC) 
    {
    }
}

Test Class:

@isTest
public class Class_Test 
{
    public TestMethod static void Class_Test_Method()
    {
        List<Opportunity> op_List = New List<Opportunity>();
        Opportunity op = new Opportunity();
        op.abc__c = 'P';
        op.name='Hello';
        op.CloseDate=date.today();
        op.StageName='Prospecting';
        op_List.add(op);
        Test.startTest();
        Database.SaveResult []str = Database.insert(op_List,false);
        system.assertEquals(True, str[0].isSuccess());
        Test_Class abcd = New Test_Class();
        Database.executeBatch(abcd);
        Test.stopTest();
    }
}
*It Shows 100% code coverage for this batch class

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com