+ Start a Discussion
sfdc007sfdc007 

test class for batch help

Hi,

I am trying to write test class for the below batch class ,I am getting the following error on my test class


System.NullPointerException: Business Hours Id cannot be null
 
MT BATCH CLASS :


global class CROBatchController implements Database.Batchable<sObject> {
    global string strBusHrId;
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        BusinessHours bh = [SELECT Id, Name FROM BusinessHours WHERE Name = 'Defend'];
        if(bh != null){
            strBusHrId = bh.Id; 
        }
        // SOQL Query to fetch the values of hours , minutes and days
        String query = 'select Id, Status__c,CreatedDate From Team_Form__c where Status__c = \'New\' and Engagement_Team__r.Initial__c = \'Defend\'';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Team_Form__c> lstTF)
    {
        system.debug('strBusHrId=>'+strBusHrId);
        List<Id> lstTFIds = new List<Id>();
        map<Id,DateTime>tfCDtMap = new map<Id,DateTime>();
        
        for(Team_Form__c objTF: lstTF){
        	lstTFIds.add(objTF.Id); 
        }
        
        for(Team_Form__History objTFHis:[select Id, CreatedDate, NewValue,OldValue,ParentId from Team_Form__History where Field = 'Status__c' and ParentId in :lstTFIds Order By CreatedDate ASC]){
            if(string.valueof(objTFHis.NewValue) == 'New'){
            	tfCDtMap.put(objTFHis.ParentId,objTFHis.CreatedDate);
            }
        }
        
        Datetime startDate, endDate;
        endDate = System.Datetime.now();        
        string subject;
        map <Id, String> chatterTextMap = new map <Id, String> ();
        map <Id, set <Id>> mentionIdMap = new map <Id, set <Id>> ();
        List<Id> lsmentionId = new List<Id>();        
        for(Team_Group_Member__c objTGM :[select Id, Engagement_Team__r.Initial__c,User__c From Team_Group_Member__c where User_Role__c = 'Super User' and Engagement_Team__r.Initial__c = 'Defend']){                
            lsmentionId.add(objTGM.User__c);
        }        
        for(Team_Form__c objTF: lstTF)
        {            
            if(tfCDtMap.get(objTF.Id) != null){
                startDate = tfCDtMap.get(objTF.Id);
            }else{
               startDate = objTF.Createddate; 
            }
			long businessMillisecondsDiff = BusinessHours.diff(strBusHrId, startDate, endDate);             
            long millitoHrs = businessMillisecondsDiff/3600000;
            if(millitoHrs >= 48){                
                subject = 'TF is in \'New\' status for more than 48hrs';
                chatterTextMap.put(objTF.Id,Subject);
                mentionIdMap.put(objTF.Id,new set <Id>(lsmentionId));
                if(chatterTextMap.size()>0){
                    CommunityUtility.postCustomFeedItem (mentionIdMap, chatterTextMap, 'AllUsers');
                }
            }
        }         
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}
MY TEST CLASS :


@isTest
Public class CROBatchController_Test{
public static testMethod void testBatch() {
 skipingHelper.SkippThemAll();
 BusinessHours bh = [SELECT Id, Name FROM BusinessHours WHERE Name = 'Defend'];
 Account acct = GPSTestUtilities.createTestAccount();
 Opportunity oppty1 = GPSTestUtilities.createTestOpportunity(acct.Id);
 Engagement_Script__c ES = GPSTestUtilities.createEngagementScript(acct.Id,oppty1.Id);  
 Engagement_Team__c ET = createEngagementTeamDefend();   
 Team_Form__c TF = createTeamFormDefend(ES.Id);  
 //Team_Group_Member__c  TFMem =createTeamGroupMemberDefend(); 
    
    Test.startTest();
	   CROBatchController mbcCon=new CROBatchController();
       //mbcCon.businessHrsId = bh.Id;
		ID batchprocessid = Database.executeBatch(mbcCon);
	Test.stopTest();
}
    	 public static Engagement_Team__c createEngagementTeamDefend() {  
        list<Engagement_Team__c> ET1 =[SELECT ID,Initial__c FROM Engagement_Team__c WHERE  Initial__c ='Defend' limit 1];
        Engagement_Team__c tmobj = new Engagement_Team__c();
        if(ET1.size()==0){
            Engagement_Team__c ET = new Engagement_Team__c();
            ET.Engagement_Model__c = 'Queue';
            ET.IsActive__c = true;
            ET.Engage_Direct__c = true;
            ET.Engage_Period__c =  'PreSales';
            ET.Engaged_By__c = 'Sales & Support';
            ET.Group_API_Name__c='EP_Defend_Group';
            ET.Queue_API_Name__c ='EP_Defend_Queue';
            ET.Super_User_Group__c='EP_Defend_SuperUser';
            ET.Initial__c ='Defend';
            ET.Team_Name__c ='Defend Team';           
            insert ET;
            tmobj =  ET;               
        }
        else{
            tmobj= ET1[0];}        
        return tmobj;
    }
    
    public static Team_Form__c createTeamFormDefend(Id ESId) {
        Engagement_Team__c ET=createEngagementTeamDefend();
        
        Engaged_Team__c engtm= new Engaged_Team__c (Engagement_Team__c=ET.Id,Engagement_Script__c=ESId);
        Insert engtm;
        
        Id rectypeid=[select Id,name from RecordType where NAME='Defend' and SobjectType='Team_Form__c' limit 1].Id;
        Team_Form__c TF = new Team_Form__c();
        TF.Engagement_Script__c = ESId;
        TF.Engagement_Team__c = ET.Id;    
        TF.Status__c='New';
        TF.RecordTypeId = rectypeid; 
        TF.OwnerId =  UserInfo.getUserId();
        TF.Revision_Reason__c = 'Cost/Price Impacting Change in Requirements';
        TF.Revision_Reason_Detail__c = 'Number of sites';
        TF.IsActive__c = true;
        insert TF;
        
        return TF;
    }

    
}


Kindly help me what is the issue here pls

Thanks