+ Start a Discussion
sfdc007sfdc007 

Test class for Batch class help needed

Hi,

I am trying to write test class for the below batch class , Kindly help me how to write the test class for it please

 
MY 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)
    {
    }
}

Kindly help me pls

Thanks in Advance
 
bob_buzzardbob_buzzard
The Apex workbook has an example test class for a batch class :

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_batch_2.htm
Tejpal KumawatTejpal Kumawat
Hello Friend,

Create your data in the following snippt :
@isTest
public with sharing class CROBatchControllerTest {
    static testmethod void myUnitTest01() {
        //Create you test database
		//Insert Team_Form__c records etc
		
        test.startTest();
            CROBatchController batchrrr = new CROBatchController();
            ID batchprocessid = database.executebatch(batchrrr);
        test.stopTest();
    }
}
Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991

If this answers your question mark Best Answer it as solution and then hit Like!


 
sfdc007sfdc007
I am getting the following error on my test class


System.NullPointerException: Business Hours Id cannot be null
 
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;
    }
   /*  public static Team_Group_Member__c createTeamGroupMemberDefend(){
        Engagement_Team__c ET=createEngagementTeamDefend();
        string strUsr;
        User usr = GPSTestUtilities.createStandardUser(strUsr);
        
        List<Team_Group_Member__c> lstTGMem =[select User_Role__c,Id,User__c from Team_Group_Member__c where User_Role__c =:'Super User' and Engagement_Team__c =:ET.id and User__c=:UserInfo.getUserId() and User__r.isActive=True];
        system.debug('lstTGMem==>'+lstTGMem);
        if(lstTGMem.size()==0)
        {        
            Team_Group_Member__c TGM= new Team_Group_Member__c (User_Role__c='Super User',Engagement_Team__c=ET.id,User__c=UserInfo.getUserId(),RecordTypeId = RecordTypeHelper.getRecordTypeId('Team_Group_Member__c','Defend Team Member'));
            Insert TGM;
            lstTGMem.add(TGM);
        }
        Team_Group_Member__c TGM1 = [select User_Role__c,Id,User__c from Team_Group_Member__c where User_Role__c =:'SA Manager' and Engagement_Team__c =:ET.id and RecordTypeId =: RecordTypeHelper.getRecordTypeId('Team_Group_Member__c','Defend Team Member') and User__r.isActive=True LIMIT 1];
        return lstTGMem[0];
    }*/
    
}


Kindly help me what is the issue here pls

Thanks
 
Tejpal KumawatTejpal Kumawat
Hello friend,

Try with this : http://salesforce.stackexchange.com/questions/33580/set-business-hours-in-apex-test-code

Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991

If this answers your question mark Best Answer it as solution and then hit Like!
sfdc007sfdc007
nopes still the same error :(
bob_buzzardbob_buzzard
Do you have a BusinessHours instance in the system? Your test will try to access the default and if you haven't set one up (through the UI, not as part of your test code) you will get this error.

If this answers your question, unlike or remove a Best Answer from someone who makes demands on every post.
sfdc007sfdc007
Yes , i do have Business hours instance in my system , i m using business hours query in my test class , but still getting the same error