0

This is my batch class . i want to write a test class for this but i am facing problem in writing test class for batch class , i am not able to enter inside the for loop of s object please help me

This is my batch class . i want to write a test class for this but i am facing problem in writing test class for batch class , i am not able to enter inside the for loop of s object please help me

global class HMT_AutomatedReviewDateBatch implements Database.Batchable<sObject> {
    global String query;
    global Database.querylocator start(Database.BatchableContext BC){
   //         query ='SELECT Id,Profile.Name FROM User ';
            return Database.getQueryLocator(query);
    }

global void execute(Database.BatchableContext BC, List<sObject> scope){
   for(sObject user : scope) {

     //Check if this profile is for HMT 
     User objUser = (User)user;
     System.debug('CLM Portfolio User : '+ user );
     HMT_Profile_set__c objProfile = HMT_Profile_set__c.getInstance(objUser.Profile.Name);

     if(objProfile!=null && objProfile.isUsedInReviewAccounts__c) {
        // Get all Managed Accounts
        List<Account> AllAccList= new List<Account>();
        Map<Id,HMT_Date__c> AssignedMap = new Map<Id,HMT_Date__c>();
        List<Account> UnasignedGCGAccList = new List<Account>();
        List<Account> UnasignedNonGCGAccList = new List<Account>();       
        List<HMT_Date__c> AccountsToUpdate = new List<HMT_Date__c>();
        Integer average = 0;
        Integer remainingRecords = 0;
        Integer recordsPointer = 0;
        Integer AssignedCount = 0;
        Integer TotalGCGUnassignedCount = 0;
        Integer TotalNonGCGUnassignedCount = 0;
        Integer TotalRecordCount = 0;
        Integer TotalGCGCount = 0;
        Integer TotalNonGCGCount = 0;
        Integer Cnt1YrGCG = 0; 
        Integer Cnt2YrGCG = 0;
        Integer Cnt1YrNonGCG = 0; 
        Integer Cnt2YrNonGCG = 0;
        Integer Cnt3YrNonGCG = 0; 

// Get the Batch year 
        Date startDate = Date.Today();
        Integer firstYear = Date.Today().year();
        if(Date.Today().month()>=10){
                startDate = Date.Today().addYears(1);
                firstYear++;
         }

// Get assigned Account list and counts         
        For (HMT_Date__c hd : [Select Account__c, Account__r.Platform_Segmentation__c, Review_Start_Date__c, Review_End_Date__c 
                               from HMT_Date__c 
                               where Account__r.OwnerId = :user.id
                               and Account__r.SE_NO__c != null 
                               and Account__r.division = '02d70000000PB4f' and Account__r.Name != 'Sales Pursuit' 
                               and Account__r.ACCT_STATUS__c != NULL And (NOT Account__r.ACCT_STATUS__c LIKE 'CANCELLED%') 
                               And Account__r.Global_Business_Center__c != '161' And Account__r.Global_Business_Center__c != '020'
                              ])
        {
          AssignedMap.put(hd.Account__c,hd);
          if ((hd.Review_Start_Date__c != null ? hd.Review_Start_Date__c : null) != null){

            if (hd.Account__r.Platform_Segmentation__c == 'GCG') {
                if (hd.Review_Start_Date__c.year()== firstYear){
                    ++Cnt1YrGCG;
                } else if (hd.Review_Start_Date__c.year()== firstYear +1){
                    ++Cnt2YrGCG;                
                }
            }
            else {
                if (hd.Review_Start_Date__c.year()== firstYear){
                    ++Cnt1YrNonGCG;
                } else if (hd.Review_Start_Date__c.year()== firstYear +1){
                    ++Cnt2YrNonGCG;             
                } else if (hd.Review_Start_Date__c.year()== firstYear +2){
                    ++Cnt3YrNonGCG;             
                }
            }
         }
        }        

// Get All Managed accounts by User and the counts of unassigned GCG & NON GCG accounts        

       for (Account acc : [SELECT Id, Name, SE_NO__c, Platform_Segmentation__c FROM Account 
                            where OwnerId = :user.Id and SE_NO__c != null and division = '02d70000000PB4f' 
                            and Name != 'Sales Pursuit' and ACCT_STATUS__c != NULL And (NOT ACCT_STATUS__c LIKE 'CANCELLED%') 
                            And Global_Business_Center__c != '161' And Global_Business_Center__c != '020'])
       {
 //             AllAccList.add(acc);
                ++TotalRecordCount; 
                if (acc.Platform_Segmentation__c == 'GCG') {
                    ++TotalGCGCount;
                    if (!AssignedMap.containsKey(acc.id)){
                        ++TotalGCGUnassignedCount;
                        UnasignedGCGAccList.add(acc);
                    }
                }
                else {
                   ++TotalNonGCGCount;
                   if (!AssignedMap.containsKey(acc.id)){
                        ++TotalNonGCGUnassignedCount;
                        UnasignedNonGCGAccList.add(acc);
                   }
                }
       }
       System.debug('Total Managed Accounts***'+TotalRecordCount);
        System.debug('AccountsToUpdate***'+AccountsToUpdate);

       //Processing the unassigned GCG Account
        if(TotalGCGUnassignedCount > 0){
            average = (TotalGCGCount/2);
            remainingRecords = Math.mod(TotalGCGCount,2);
            recordsPointer = 0;

            System.debug('GCG Account Processing' );
            System.debug('TotalGCGRecordCount***'+TotalGCGCount);
            System.debug('TotalGCGUnassignedCount ***'+TotalGCGUnassignedCount );
            System.debug('New Average for 2 years ***'+average);
            System.debug('Assigned Year 1 ***'+Cnt1YrGCG );
            System.debug('Assigned Year 2 ***'+Cnt2YrGCG );

            //Start assigning review dates from first year
            if(average-Cnt1YrGCG>0 && TotalGCGUnassignedCount>0){
                if(average-Cnt1YrGCG<TotalGCGUnassignedCount){
                    for(Integer counter=0;counter<(average-Cnt1YrGCG);counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate,
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                    }       
                    AssignedCount = average-Cnt1YrGCG;
                    recordsPointer = average-Cnt1YrGCG;
                    TotalGCGUnassignedCount = TotalGCGUnassignedCount - (average-Cnt1YrGCG);  
                }
                else{
                    for(Integer counter=0;counter<TotalGCGUnassignedCount;counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate,
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                    }       
                    AssignedCount = TotalGCGUnassignedCount;
                    recordsPointer = TotalGCGUnassignedCount;
                    TotalGCGUnassignedCount = 0;    
                }
                System.debug('Records Assigned 1st year***'+ AssignedCount);
                System.debug('Left Over NonGCG Unassigned Count After 1st year***'+TotalGCGUnassignedCount);   
            }

            //Start assigning review dates from Second year
            if(average-Cnt2YrGCG>0 && TotalGCGUnassignedCount>0){
                if(average-Cnt2YrGCG<TotalGCGUnassignedCount){
                    for(Integer counter=recordsPointer;counter<recordsPointer+(average-Cnt2YrGCG);counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate.addYears(1),
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                    }       
                    AssignedCount = average-Cnt2YrGCG;
                    recordsPointer = recordsPointer + average-Cnt2YrGCG;
                    TotalGCGUnassignedCount = TotalGCGUnassignedCount - (average-Cnt2YrGCG);  
                }
                else{
                    for(Integer counter=recordsPointer;counter<recordsPointer+TotalGCGUnassignedCount;counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate.addYears(1),
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                    }       
                    AssignedCount = TotalGCGUnassignedCount;
                    recordsPointer = recordsPointer + TotalGCGUnassignedCount;
                    TotalGCGUnassignedCount = 0;  
                }
                System.debug('Records Assigned 2nd year***'+AssignedCount);
                System.debug('Left Over NonGCG Unassigned Count After 2nd year***'+TotalGCGUnassignedCount);     
            }

            //Check if there were more records which are unassigned
            if(TotalGCGUnassignedCount>0 || remainingRecords>0){
                for(Integer counter=recordsPointer;counter<UnasignedGCGAccList.size();counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate.addYears(1),
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                }
                AssignedCount = UnasignedGCGAccList.size() - recordsPointer;
                System.debug('Remaining Records Assigned to 2nd Year ***'+AssignedCount);
            }

        }
         else {
            System.debug('No GCG Account Processing Required' );
            System.debug('TotalGCGRecordCount***'+TotalGCGCount);
            System.debug('TotalGCGUnassignedCount ***'+TotalGCGUnassignedCount );
            System.debug('Assigned Year 1 ***'+Cnt1YrGCG );
            System.debug('Assigned Year 2 ***'+Cnt2YrGCG );
         }

       //Processing the unassigned NON GCG Account
        if(TotalNonGCGUnassignedCount > 0){
            average = (TotalNonGCGCount/3);
            remainingRecords = Math.mod(TotalNonGCGCount,3);
            recordsPointer = 0;            
            System.debug('Non GCG Account Processing' );
            System.debug('TotalNonGCGRecordCount***'+TotalNonGCGCount);
            System.debug('TotalNonGCGUnassignedCount ***' +TotalNonGCGUnassignedCount );
            System.debug('New Average for 2 years ***'+average);
            System.debug('Assigned Year 1 ***'+Cnt1YrNonGCG );
            System.debug('Assigned Year 2 ***'+Cnt2YrNonGCG );
            System.debug('Assigned Year 3 ***'+Cnt3YrNonGCG );

            //Start assigning review dates from first year
            if(average-Cnt1YrNonGCG>0 && TotalNonGCGUnassignedCount>0){
                if(average-Cnt1YrNonGCG<TotalNonGCGUnassignedCount){
                    for(Integer counter=0;counter<(average-Cnt1YrNonGCG);counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate,
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                    }       
                    AssignedCount = average-Cnt1YrNonGCG;
                    recordsPointer = average-Cnt1YrNonGCG;
                    TotalNonGCGUnassignedCount = TotalNonGCGUnassignedCount - (average-Cnt1YrNonGCG);  
                }
                else{
                    for(Integer counter=0;counter<TotalNonGCGUnassignedCount;counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate,
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                    }       
                    AssignedCount = TotalNonGCGUnassignedCount;
                    recordsPointer = TotalNonGCGUnassignedCount;
                    TotalNonGCGUnassignedCount = 0;    
                }

                System.debug('Records Assigned 1st year***'+ AssignedCount);
                System.debug('Left Over NonGCG Unassigned Count After 1st year***'+ TotalNonGCGUnassignedCount);   
            }

            if(average-Cnt2YrNonGCG>0 && TotalNonGCGUnassignedCount>0){
                if(average-Cnt2YrNonGCG<TotalNonGCGUnassignedCount){
                    for(Integer counter=recordsPointer;counter<recordsPointer+(average-Cnt2YrNonGCG);counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate.addYears(1),
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                    }       
                    AssignedCount = average-Cnt2YrNonGCG;
                    recordsPointer = recordsPointer + average-Cnt2YrNonGCG;
                    TotalNonGCGUnassignedCount = TotalNonGCGUnassignedCount - (average-Cnt2YrNonGCG);  
                }
                else{
                    for(Integer counter=recordsPointer;counter<recordsPointer+TotalNonGCGUnassignedCount;counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate.addYears(1),
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                    }       
                    AssignedCount = TotalNonGCGUnassignedCount;
                    recordsPointer = recordsPointer + TotalNonGCGUnassignedCount;
                    TotalNonGCGUnassignedCount = 0;  
                }

                System.debug('Records Assigned 2st year***'+ AssignedCount);
                System.debug('Left Over NonGCG Unassigned Count After 2st year***'+TotalNonGCGUnassignedCount);   
            }

            if(average-Cnt3YrNonGCG>0 && TotalNonGCGUnassignedCount>0){
                if(average-Cnt3YrNonGCG<TotalNonGCGUnassignedCount){
                    for(Integer counter=recordsPointer;counter<recordsPointer+(average-Cnt3YrNonGCG);counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate.addYears(2),
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                    }       
                    AssignedCount = average-Cnt3YrNonGCG;
                    recordsPointer = recordsPointer + average-Cnt3YrNonGCG;
                    TotalNonGCGUnassignedCount = TotalNonGCGUnassignedCount - (average-Cnt3YrNonGCG);    
                }
                else{
                    for(Integer counter=recordsPointer;counter<recordsPointer+TotalNonGCGUnassignedCount;counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate.addYears(2),
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                    }       
                    AssignedCount = TotalNonGCGUnassignedCount;
                    recordsPointer = recordsPointer + TotalNonGCGUnassignedCount;
                    TotalNonGCGUnassignedCount = 0;  
                }
                System.debug('Records Assigned to 3rd year***'+ AssignedCount);
                System.debug('TotalNonGCGUnassignedCount 3rd year***'+TotalNonGCGUnassignedCount);               
            }

            //Check if there were more records which are unassigned
            if(TotalNonGCGUnassignedCount>0 && remainingRecords>0){
                for(Integer counter=recordsPointer;counter<UnasignedNonGCGAccList.size();counter++){
                        AccountsToUpdate.add( new HMT_Date__c (Account__c = UnasignedNonGCGAccList[counter].Id, 
                                                               Review_Start_Date__c = startDate.addYears(2),
                                                               SE_NO__c = UnasignedNonGCGAccList[counter].SE_NO__c
                                                              ));
                        System.debug('AccountsToUpdate***'+ AccountsToUpdate);
                }
                AssignedCount = UnasignedNonGCGAccList.size() - recordsPointer;
                System.debug('Remaining Records Assigned to 3nd Year ***'+AssignedCount);
            }
        } // End of IF NON GCG  
         else {
            System.debug('No Non GCG Account Processing Required' );
            System.debug('TotalNonGCGRecordCount***'+TotalNonGCGCount);
            System.debug('TotalNonGCGUnassignedCount ***' +TotalNonGCGUnassignedCount );
            System.debug('Assigned Year 1 ***'+Cnt1YrNonGCG );
            System.debug('Assigned Year 2 ***'+Cnt2YrNonGCG );
            System.debug('Assigned Year 3 ***'+Cnt3YrNonGCG );
         }

//        System.debug('AccountsToUpdate***'+AccountsToUpdate);
        if(AccountsToUpdate!=null && AccountsToUpdate.size()>0){
           insert AccountsToUpdate;
        System.debug('AccountsToUpdate***'+AccountsToUpdate);
        }

    }  // End of If for HMT Profile check 
    }  // End of for loop 
 }  

global void finish(Database.BatchableContext BC){
      System.debug('Batch Process Complete');
    } 
}

1 Answer 1

0

I guess your usual test writing approach works fine here.

@isTest
private class myTestClass {

static testMethod void myTestMethod() {
   generateTestData();
   Test.startTest();
   HMT_AutomatedReviewDateBatch batchObj = new HMT_AutomatedReviewDateBatch();
   batchObj.execute();
   Test.stopTest();
}

}

11
  • no the usual test writing approach is not working , i wrote this but it is not working @isTest private class test_HMT_AutomatedReviewDateBatch { static testMethod void myUnitTest() { Test.StartTest(); List<User> scope=[SELECT Id,Profile.Name FROM User limit 10]; Database.BatchableContext bc; HMT_AutomatedReviewDateBatch pr= new HMT_AutomatedReviewDateBatch(); pr.start(bc); pr.execute(bc,scope); pr.finish(bc); Test.stopTest(); } }
    – megha jain
    Jul 16, 2013 at 13:09
  • no the usual test writing approach is not working , i wrote this but it is not working @isTest private class test_HMT_AutomatedReviewDateBatch { static testMethod void myUnitTest() { Test.StartTest(); List<User> scope=[SELECT Id,Profile.Name FROM User limit 10]; Database.BatchableContext bc; HMT_AutomatedReviewDateBatch pr= new HMT_AutomatedReviewDateBatch(); pr.start(bc); pr.execute(bc,scope); pr.finish(bc); Test.stopTest(); }}
    – megha jain
    Jul 16, 2013 at 13:21
  • my bad. You don't need to call all methods manually, you only need to start batch as you would usually start it: Database.executeBatch(myBatchObj) here is the offical doc link Jul 16, 2013 at 13:21
  • Not able to understand hoe to o it , i tried to enter the that list bu i am not ablt to
    – megha jain
    Jul 16, 2013 at 13:25
  • Can you provide me some exact lines of code to enter inside that s scope object loop
    – megha jain
    Jul 16, 2013 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.