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
vijay T 37vijay T 37 

I have one batch class Unable to write test class

 This is Batch Class 
global class BatchAccountDescUpdate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT description FROM Account';
        return Database.getQueryLocator(query);
        
        //Even we can write like below also
        //return Database.QueryLocator('SELECT description FROM Account')
    }
    global void execute(Database.BatchableContext BC, List<Account> scope){ 
        
        for(Account a: scope){
            if(a.description == 'Not Available'){
               a.Description = 'Please fill details, else record will be made inactive!';
              
            }
            Update scope;       
        }
    }  
    global void finish(Database.BatchableContext BC){
        //code for sending email
        //calling another batch 
        //sending state of processing records ** using stateful 
    }

}

Below is my Test Class
@isTest
public class BatchAccountDescUpdateTest
{
    static testMethod void testMethod1()
    {
        List<Account> lstAccount = new List<Account>();
        for(Integer i=0 ;i <200;i++)
        {
            Account acc = new Account();
            if(acc.description == 'Not Available'){
               acc.description = 'Please fill details, else record will be made inactive!';
             }
            lstAccount.add(acc);
        }
       
        insert lstAccount;
       
        Test.startTest();

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


 
Abdul KhatriAbdul Khatri
Hi Vijay

Try this, The issue was in the creation of account, not sure why you have if check.
@isTest
public class BatchAccountDescUpdateTest
{
    static testMethod void testMethod1()
    {
        List<Account> lstAccount = new List<Account>();
        for(Integer i=0 ;i <200;i++)
        {
            Account acc = new Account();
            acc.Name = 'Test-' + String.valueOf(i);
            acc.description == 'Not Available'
            lstAccount.add(acc);
        }
       
        insert lstAccount;
       
        Test.startTest();
        BatchAccountDescUpdate obj = new BatchAccountDescUpdate();
        DataBase.executeBatch(obj);
        Test.stopTest();
        
        List<Account> accountList = [SELECT Id FROM Account WHERE Description = 'Not Available'];
        system.assert(accountList.size()==0);
    }
}
sachinarorasfsachinarorasf
Hi Vijay,

I have gone through your problem. 

Here is the code you can use it.

@isTest
public class BatchAccountDescUpdate_Test 
{
    static testMethod void batchAccount_Test()
    {
        List<Account> accountList = new List<Account>();
        Account acc = new Account();
        acc.Name = 'Test';
        acc.description = 'Not Available';
        accountList.add(acc);
        insert accountList;
       
        Test.startTest();
        BatchAccountDescUpdate batchObj = new BatchAccountDescUpdate();
        DataBase.executeBatch(batchObj);
        Test.stopTest();
    }
}

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

Thanks and Regards,
Sachin Arora