How to write a test class for this batch class? - Answers - Salesforce Trailblazer Community
Ask Search:
Kavya Sree MareeduKavya Sree Mareedu 

How to write a test class for this 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 Sree Mareedu
Mohammadsohail PatelMohammadsohail Patel
Hi Kavya,
Please do try the below code:
 
@IsTest
public class OTFAutomationReport _Test {
        
    static testmethod void TestOTFAutomationReport(){
        List<Account> Acclist = new List<Account>();
        //Account creation
        Account acc1 = Create account();
        Acclist.add(acc1);
        Account acc2 = Create account();
        Acclist.add(acc2);
        
        Insert Acclist;
        
        List<Time_Logs__c> timelogslist = new List<Time_Logs__c>();
        Time_Logs__c TL1 = create a time log with acc1.id;
        Time_Logs__c TL2 = create a time log with acc1.id;
        
        Time_Logs__c TL3 = create a time log with acc2.id;
        Time_Logs__c TL4 = create a time log with acc2.id;
   
        //Executing the batch class
        Test.startTest();
                Database.executeBatch(new OTFAutomationReport ());
        Test.stopTest();
    }

}

 

All Answers

Mohammadsohail PatelMohammadsohail Patel
Hi Kavya,
Please do try the below code:
 
@IsTest
public class OTFAutomationReport _Test {
        
    static testmethod void TestOTFAutomationReport(){
        List<Account> Acclist = new List<Account>();
        //Account creation
        Account acc1 = Create account();
        Acclist.add(acc1);
        Account acc2 = Create account();
        Acclist.add(acc2);
        
        Insert Acclist;
        
        List<Time_Logs__c> timelogslist = new List<Time_Logs__c>();
        Time_Logs__c TL1 = create a time log with acc1.id;
        Time_Logs__c TL2 = create a time log with acc1.id;
        
        Time_Logs__c TL3 = create a time log with acc2.id;
        Time_Logs__c TL4 = create a time log with acc2.id;
   
        //Executing the batch class
        Test.startTest();
                Database.executeBatch(new OTFAutomationReport ());
        Test.stopTest();
    }

}

 
This was selected as the best answer
Kavya Sree MareeduKavya Sree Mareedu
Thanks for the help!
Mohammadsohail PatelMohammadsohail Patel
Always there to help :) 
 
Vyadham VijayVyadham Vijay
Hi

How to write a test class for this trigger

Trigger: 
trigger TriggerApprover on opportunity (before update) {
    
       if(trigger.isUpdate){
             List<Opportunity> opptyList =  [Select id,
                                                   (Select Id, 
                                                         IsPending, 
                                                         ProcessInstanceId, 
                                                         TargetObjectId, 
                                                         StepStatus, 
                                                         OriginalActorId, 
                                                         ActorId, 
                                                         RemindersSent, 
                                                         Comments, 
                                                         IsDeleted, 
                                                         CreatedDate, 
                                                         CreatedById, 
                                                         SystemModstamp 
                                                    FROM ProcessSteps
                                                ORDER BY CreatedDate DESC) 
                                                    From opportunity
                                                WHERE Id IN : Trigger.new];

             if(opptyList.size() > 0){

               for(Opportunity opp : opptyList){
              
                for(Opportunity opp1 : Trigger.new) {
                  
                         //check copy comment is true
                         if(opp.id == opp1.id && opp1.copy_comment__c) {
 
                           if (opp.ProcessSteps.size() > 0) {
                            
                         opp1.Reason_for_Approval_Rejection__c = opp.ProcessSteps[0].Comments;
                         //opp1.copy_comment__c = false;//
                
                           }

                        }
                 
                    }
               }
             }   
        }  
    }

 
Mohammadsohail PatelMohammadsohail Patel
Hi Vyadham,
@IsTest
public class TriggerApprover _Test {
        
    static testmethod void TestTriggerApprover(){
        List<Opportunity> Opptylistinsert = new List<Opportunity>();
        //Account creation
        Opportunity opp1= Create Opportunity ();
        Opptylistinsert .add(opp1);
        Opportunity opp2= Create Opportunity ();
        Opptylistinsert .add(opp2);
        Insert Opptylistinsert ;
         
        List<Opportunity> Opptylistupdate = new List<Opportunity>(); 
        Update the field values of opp1 as per the trigger condition;
        Opptylistupdate.add(opp1);
        Update the field values of opp2 as per the trigger condition;
        Opptylistupdate.add(opp2);
        //Executing the batch class
        Test.startTest();
               Update Opptylistupdate;
        Test.stopTest();
    }

}