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
kavya mareedukavya mareedu 

How to write test class for the below batch class?

global class OTFAutomationReport implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        //String query = 'select Id,OTF_Total_Hours__c,OTFClient__c, OTF_Date__c, (select Date__c, Duration_Hours__c from Time_Logs__r) from account where Name='+80 Acres Farms+';
       
        
        system.debug('==query==>' +[select Id,OTF_Total_Hours__c,OTFClient__c, OTF_Date__c, (select Date__c, Duration_Hours__c from Time_Logs__r) from account where Name='80 Acres Farms']);
        return Database.getQueryLocator([select Id,OTF_Total_Hours__c,OTFClient__c, OTF_Date__c, (select Date__c, Duration_Hours__c from Time_Logs__r) from account where Name='80 Acres Farms']);
       
    }
    global void execute(Database.BatchableContext bc, List<account> scope)
    {
        system.debug('==scope==>' +scope);
        //decimal TotalHours=0.0;
        list<account> accUpdate = new list<account>();
        for(account a: scope)
           
        {
            a.OTF_Total_Hours__c=0.0;
            system.debug('a===>1st loop' +a);
            for(Time_Log__c tl : a.Time_Logs__r)
            { system.debug('a===>2nd loop' +tl);
            
                //system.debug('a==a.OTF_Total_Hours__c=>2nd loop' +a.OTF_Total_Hours__c);
             system.debug('a=tl.Duration_Hours__c==>2nd loop' +tl.Duration_Hours__c);
            
                if(tl.Duration_Hours__c!=NUll && a.OTF_Total_Hours__c!=Null){
                a.OTF_Total_Hours__c +=tl.Duration_Hours__c;
                system.debug('==TotalHours==>' +a.OTF_Total_Hours__c);
                if(a.OTF_Total_Hours__c>=20 && a.OTFClient__c==false)
                {
                    a.OTFClient__c=true;
                    system.debug('==a.OTFClient__c==>' +a.OTFClient__c);
                    a.OTF_Date__c=tl.Date__c;
                    accUpdate.add(a);
                }
                }
            }
           
            
        }
        system.debug('==accUpdate > ' +accUpdate);
        update accUpdate;
       
    }
    global void finish(Database.BatchableContext BC)
    {}   
    
}
 
Best Answer chosen by kavya mareedu
Raghu NaniRaghu Nani
Hi Below is the sample test class...
Please insert the data from test class and invoke the batch apex...

@isTest 
public class ExampleTestClass
{
    static testMethod void testMethod1() 
    {
      
      // create an account
        Account MyAccount = new Account(Name='Test');
        insert MyAccount;

     // Create a case on the test account
        date myDate = date.today();
        Account MyCase = new Account(Name='Test');
        MyCase.AccountId = MyAccount.Id;    
        insert MyCase;
        
        Test.startTest();
        BatchClassName batch = new BatchClassName();
        Database.executeBatch(batch );
        Test.stopTest();
      
    }
}

All Answers

Raghu NaniRaghu Nani
Hi Below is the sample test class...
Please insert the data from test class and invoke the batch apex...

@isTest 
public class ExampleTestClass
{
    static testMethod void testMethod1() 
    {
      
      // create an account
        Account MyAccount = new Account(Name='Test');
        insert MyAccount;

     // Create a case on the test account
        date myDate = date.today();
        Account MyCase = new Account(Name='Test');
        MyCase.AccountId = MyAccount.Id;    
        insert MyCase;
        
        Test.startTest();
        BatchClassName batch = new BatchClassName();
        Database.executeBatch(batch );
        Test.stopTest();
      
    }
}
This was selected as the best answer
kavya mareedukavya mareedu
Thanks Raghu!