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
vijendhar k 23vijendhar k 23 

how to Cover start mehod in batch class

Hello All,
how to get 100% code coverage for bello batch classs, i am getting 50% i have needed 100% coverage
global class Test implements Database.Batchable<sObject>
{
    global Database.Querylocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator('');
    }
    global void execute(Database.batchableContext BC, List<sObject> batch) {}
    global void finish(Database.batchableContext info) {}
}
below test class
 
public static  testMethod void Unit1()
    {
         test.startTest();
        Database.BatchableContext BC;
        Database.BatchableContext info;
        list<sObject> so = new list<sObject>();
        Test ac = new Test();
        //ac.start(BC);
        ac.execute(BC,so);
        ac.Finish(info);  
        test.stoptest();
    }
help me get 100% coverage for below class

Thanks
 
Best Answer chosen by vijendhar k 23
Amit Chaudhary 8Amit Chaudhary 8
To Call Batch job in Test class use below code
public static  testMethod void Unit1()
    {
        test.startTest();
                 Test obj = new Test ();
                 DataBase.executeBatch(obj);
        test.stoptest();
    }




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

Batch Apex in salesforce | Test class for Batch job | how to schedule 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) {
    }
}
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

All Answers

Amit Chaudhary 8Amit Chaudhary 8
To Call Batch job in Test class use below code
public static  testMethod void Unit1()
    {
        test.startTest();
                 Test obj = new Test ();
                 DataBase.executeBatch(obj);
        test.stoptest();
    }




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

Batch Apex in salesforce | Test class for Batch job | how to schedule 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) {
    }
}
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
This was selected as the best answer
vijendhar k 23vijendhar k 23
Thanks Amit :)