Salesforce 如何为批处理apex类编写测试类

Salesforce 如何为批处理apex类编写测试类,salesforce,Salesforce,这是我的批处理类。我想为此编写一个测试类,但我在为批处理类编写测试类时遇到了问题,我无法进入s对象的for循环,请帮助我 global class HMT_AutomatedReviewDateBatch implements Database.Batchable<sObject> { global String query; global Database.querylocator start(Database.BatchableContext BC){ //

这是我的批处理类。我想为此编写一个测试类,但我在为批处理类编写测试类时遇到了问题,我无法进入s对象的for循环,请帮助我

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');
    } 
}
这是我的批处理类。我想为此编写一个测试类,但我在为批处理类编写测试类时遇到了问题,我无法进入s对象的for循环,请帮助我

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');
    } 
}
全局类HMT_AutomatedRevewDateBatch实现数据库。可批处理{
全局字符串查询;
global Database.querylocator启动(Database.BatchableContext BC){
//query='SELECT Id,Profile.Name FROM User';
返回数据库.getQueryLocator(查询);
}
全局void execute(Database.BatchableContext BC,列表范围){
for(sObject用户:范围){
//检查此配置文件是否适用于HMT
用户对象=(用户)用户;
System.debug('CLM公文包用户:'+用户);
HMT\u Profile\u set\u c objProfile=HMT\u Profile\u set\u c.getInstance(objUser.Profile.Name);
if(objProfile!=null&&objProfile.isUsedInReviewAccounts\uu\c){
//获取所有托管帐户
List AllAccList=新列表();
Map AssignedMap=新映射();
List UNASignedCGACCLIST=新列表();
List UnasignedNonGCGAccList=新列表();
List AccountsToUpdate=新列表();
整数平均值=0;
整数remainingRecords=0;
整数recordsPointer=0;
整数赋值计数=0;
整数TotalGCGUnassignedCount=0;
整数TotalNonGCGUnassignedCount=0;
整数TotalRecordCount=0;
整数totalgcount=0;
整数TotalNonGCGCount=0;
整数Cnt1YrGCG=0;
整数Cnt2YrGCG=0;
整数Cnt1YrNonGCG=0;
整数Cnt2YrNonGCG=0;
整数Cnt3YrNonGCG=0;
//获取批次年
Date startDate=日期。今天();
整数firstYear=Date.Today().year();
如果(日期今天()月份()>=10){
startDate=日期。今天()。添加年份(1);
第一年++;
}
//获取分配的帐户列表和计数
对于(HMT_日期_______________________________c)hd:[选择帐户____________________
从HMT\U日期\uu\c
其中Account\uu r.OwnerId=:user.id
和帐户\uuuu r.SE\u NO\uuuuuu c!=null
和Account_ur.division='02d70000000PB4f'和Account_ur.Name!='Sales Pursuit'
和帐户\uuuu r.ACCT\u STATUS\uuuu c!=NULL和(不是帐户\uu r.ACCT\u STATUS\uu c,如“已取消%”)
账户uu r.Global u Business u Center u c!=“161”和账户u r.Global u Business u Center u c!=“020”
])
{
分配的MAP.put(hd.Account\uuu c,hd);
如果((hd.Review\u Start\u Date\u c!=null?hd.Review\u Start\u Date\u c:null)!=null){
如果(hd.Account\uuu r.Platform\u Segmentation\uuu c=='GCG'){
如果(hd.Review\u Start\u Date\u c.year()=第一年){
++Cnt1YrGCG;
}否则如果(hd.Review\u Start\u Date\u c.year()==第一年+1){
++Cnt2YrGCG;
}
}
否则{
如果(hd.Review\u Start\u Date\u c.year()=第一年){
++Cnt1YrNonGCG;
}否则如果(hd.Review\u Start\u Date\u c.year()==第一年+1){
++Cnt2YrAg;
}否则如果(hd.Review\u Start\u Date\u c.year()==第一年+2){
++cnt3yr;
}
}
}
}        
//按用户获取所有托管帐户以及未分配GCG和非GCG帐户的计数
对于(帐户acc:[从帐户中选择Id、名称、序号、平台分割
其中OwnerId=:user.Id和SE_NO__c!=null,除法='02d70000000PB4f'
名称!=“销售追踪”和账户状态!=空和(不是账户状态,如“取消%”)
和全球商业中心(分别为“161”和“020”)
{
//新增(acc);
++总记录计数;
如果(根据平台划分){
++总计数;
如果(!AssignedMap.containsKey(acc.id)){
++总计数;
未分配的CGACCLIST.add(acc);
}
}
否则{
++总计数;
如果(!AssignedMap.containsKey(acc.id)){
++总未分配计数;
unassignednongcacclist.add(acc);
}
}
}
System.debug('Total Managed Accounts***'+TotalRecordCount);
系统调试('AccountsToUpdate***'+AccountsToUpdate);
//处理未分配的GCG帐户
如果(TotalGCGUnassignedCount>0){
平均值=(总计数/2);
remainingRecords=Math.mod(TotalGCount,2);
recordsPointer=0;
系统调试(“GCG账户处理”);
系统调试('TotalGCGRecordCount***'+totalgccount);
系统调试('TotalGCGUnassignedCount***'+TotalGCGUnassignedCount);
系统调试(‘2年新平均值***’+平均值);
系统调试('分配的第1年***'+CNT1YRCG);
系统调试('分配的第2年***'+CNT2年GCG);
//从第一年开始指定审核日期
如果(平均值-Cnt1YrGCG>0&&TotalGCGUnassignedCount>0){
if(平均值-CNT1YRGG0){

如果(average-cnt1yrongcg我想你通常的测试写作方法在这里效果不错

@isTest
private class myTestClass {

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