+ Start a Discussion
GeetuGeetu 

Test class for Batch Job

Can somebody pls help me to write test class : 


global class SendEmailNotification30days_CLM implements Database.Batchable < sObject >, Schedulable, Database.Stateful
{
    global List<String> errorMessages = new List<String>();
    global Database.QueryLocator start(Database.BatchableContext bc) {
         System.debug(Date.today().addDays(30));
         Date todaysdt = Date.today(); 
         Date ed = Date.today().addDays(30);
         System.debug(Date.today().addDays(30));
         // This set will hold the Id's of the docu sign receipient status
         Set <ID> DocusignId = new Set <ID>(); 
        
         // Query all the docu sign receipient status in your database that meet the selection criterial for update.
         // Return this list of docu sign receipient status, which will get passed to our execute() method below.
         Date past30Date = Date.today().addDays(-30);
         system.debug('===past30Date==='+past30Date);
         return Database.getQueryLocator('Select  ID, Name,Customer_Flag_CLM__c,Customer_Signed_Date_CLM__c,User_Flag_CLM__c,Apttus_DocuApi__DocuSignEnvelopeId__c FROM Apttus_DocuApi__DocuSignEnvelopeRecipientStatus__c  WHERE Customer_Signed_Date_CLM__c =: past30Date and User_Flag_CLM__c = false');
}
    
    
global void execute(Database.BatchableContext bc, List < Apttus_DocuApi__DocuSignEnvelopeRecipientStatus__c > recs) 
{
        system.debug('===recs==='+recs);
        Map<Id,Id> envelopeAgreementMap = new Map<Id,Id>();
        Set<Id> envIdSet = new set<Id>();
        Set<Id> agrementIdSet = new set<Id>();
        Set<Id> appSet = new Set<Id>();
        for(Apttus_DocuApi__DocuSignEnvelopeRecipientStatus__c status : recs)
        {
            envIdSet.add(status.Apttus_DocuApi__DocuSignEnvelopeId__c);
        }
        List<Apttus_DocuApi__DocuSignEnvelope__c> DocuSignEnvelopeList =
                [SELECT Id,
                        Apttus_CMDSign__Agreement__r.Id
                 FROM Apttus_DocuApi__DocuSignEnvelope__c WHERE Id IN :envIdSet
                 ];
        for(Apttus_DocuApi__DocuSignEnvelope__c env : DocuSignEnvelopeList)
        {
            agrementIdSet.add(env.Apttus_CMDSign__Agreement__r.Id);
        }
        set<Id> agreementSet = new set<Id>();
        Set<String> ownerIds= new Set<String>();
        Set<String> ManagerName= new Set<String>();
        Map<Id,string> AgreeOwnerMap = new Map<Id,string>();
        Map<Id,string> AgreeManagerMap = new Map<Id,string>();
        List<Apttus__APTS_Agreement__c> agrList = new List<Apttus__APTS_Agreement__c>();
        for(Apttus__APTS_Agreement__c agreement : [SELECT Id,OwnerId,Owner.Name,Name,Manager_Full_Name_CLM__c
                                                   FROM Apttus__APTS_Agreement__c WHERE Id IN :agrementIdSet])
        {
            System.debug('Hello');                                           
            agreementSet.add(agreement.Id);
            agrList.add(agreement);
            ownerIds.add(agreement.OwnerId);    
            AgreeOwnerMap.put(agreement.Id,agreement.OwnerId);          
        }
    Map<Id,Id> OwnerManagerIdMap =new map<Id,Id>();
    Map<Id,User> OwnerDetailMap =new map<Id,User>();
    Set<Id> allUserIds = new Set<Id>();
            for(User userObj : [SELECT Id,Name,ManagerId,Manager.ManagerId,Manager.Manager.ManagerId,Manager.Manager.Manager.ManagerId,Manager.Manager.Manager.Manager.ManagerId,Manager.Manager.Manager.Manager.Manager.ManagerId,Manager.Manager.Manager.Manager.Manager.Manager.ManagerId FROM User WHERE Id IN :ownerIds])
             {
                 OwnerDetailMap.put(userObj.Id, userObj);
               OwnerManagerIdMap.put(userObj.Id,userObj.ManagerId);
                 allUserIds.add(userObj.ManagerId);
                 allUserIds.add(userObj.Manager.ManagerId);
                 allUserIds.add(userObj.Manager.Manager.ManagerId);
                 allUserIds.add(userObj.Manager.Manager.Manager.ManagerId);
                 allUserIds.add(userObj.Manager.Manager.Manager.Manager.ManagerId);
                 allUserIds.add(userObj.Manager.Manager.Manager.Manager.Manager.ManagerId);
                 allUserIds.add(userObj.Manager.Manager.Manager.Manager.Manager.Manager.ManagerId);
             }
            Map<Id,User> managerUserDetailMap = new Map<Id,User>([Select id,Name,Email,IsActive,Approval_Limit__c,Comp_Class__c from User where id in : allUserIds]);

            EmailTemplate empID = [SELECT Id,Name,HTMLValue,Body,Subject FROM EmailTemplate WHERE Name =: 'Test_Template' Limit 1];
            
            //send single emails to these contacts
            List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > (); 
            List<String> toAddresses = new List < String > ();
   
            for (Apttus__APTS_Agreement__c agr : agrList)
            {
                   
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                 
                    
                Id manager1Id = OwnerManagerIdMap.get(agr.ownerId);
                Id manager2Id = OwnerDetailMap.get(agr.ownerId).Manager.ManagerId;
                Id manager3Id = OwnerDetailMap.get(agr.ownerId).Manager.Manager.ManagerId;
                Id manager4Id = OwnerDetailMap.get(agr.ownerId).Manager.Manager.Manager.ManagerId;
                Id manager5Id = OwnerDetailMap.get(agr.ownerId).Manager.Manager.Manager.Manager.ManagerId;
                Id manager6Id = OwnerDetailMap.get(agr.ownerId).Manager.Manager.Manager.Manager.Manager.ManagerId;
                Id manager7Id = OwnerDetailMap.get(agr.ownerId).Manager.Manager.Manager.Manager.Manager.Manager.ManagerId;
                
               String emailStr = '';
               String ManagerFullName = '';
                if(managerUserDetailMap.get(manager1Id) != null && managerUserDetailMap.get(manager1Id).isActive){
                    emailStr = managerUserDetailMap.get(manager1Id).Email;
                    ManagerFullName = managerUserDetailMap.get(manager1Id).Name;
                }else if(managerUserDetailMap.get(manager2Id) != null && managerUserDetailMap.get(manager2Id).isActive){
                    emailStr = managerUserDetailMap.get(manager2Id).Email;
                    ManagerFullName = managerUserDetailMap.get(manager2Id).Name;
                }else if(managerUserDetailMap.get(manager3Id) != null && managerUserDetailMap.get(manager3Id).isActive){
                    emailStr = managerUserDetailMap.get(manager3Id).Email;
                    ManagerFullName = managerUserDetailMap.get(manager3Id).Name;
                }else if(managerUserDetailMap.get(manager4Id) != null && managerUserDetailMap.get(manager4Id).isActive){
                    emailStr = managerUserDetailMap.get(manager4Id).Email;
                    ManagerFullName = managerUserDetailMap.get(manager4Id).Name;
                }else if(managerUserDetailMap.get(manager5Id) != null && managerUserDetailMap.get(manager5Id).isActive){
                    emailStr = managerUserDetailMap.get(manager5Id).Email;
                    ManagerFullName = managerUserDetailMap.get(manager5Id).Name;
                }else if(managerUserDetailMap.get(manager6Id) != null && managerUserDetailMap.get(manager6Id).isActive){
                    emailStr = managerUserDetailMap.get(manager6Id).Email;
                    ManagerFullName = managerUserDetailMap.get(manager6Id).Name;
                }else if(managerUserDetailMap.get(manager7Id) != null && managerUserDetailMap.get(manager7Id).isActive){
                    emailStr = managerUserDetailMap.get(manager7Id).Email;
                    ManagerFullName = managerUserDetailMap.get(manager7Id).Name;
                }
                toAddresses.add(emailStr);
                    mail.setToAddresses(toAddresses);
                    mail.setTemplateId(empID.Id);
                    mail.setWhatId(agr.Id); 
                    String hSubject = empID.Subject;
                    hSubject = hSubject.replace('{!Apttus__APTS_Agreement__c.Name}', agr.Name);     
                    mail.setSubject(hSubject);
                    mail.setBccSender(false);
                    mail.setUseSignature(false);                
                    String hBody = empID.Body;
                    hBody = hBody.replace('{!Apttus__APTS_Agreement__c.Name}', agr.Name);
                    hBody = hBody.replace('{!Apttus__APTS_Agreement__c.CreatedBy}', agr.Owner.Name);
                   // hBody = hBody.replace('{!Apt3tus__APTS_Agreement__c.OwnerFullName}', agr.Owner.Name);
                    hBody = hBody.replace('{!Apttus__APTS_Agreement__c.Manager_Full_Name_CLM__c}', ManagerFullName);
                    mail.setPlainTextBody(hBody);
                    mailList.add(mail);
                    toAddresses.clear();
                }
             Messaging.sendEmail(mailList);
       
}

    global void finish(Database.BatchableContext bc)
{
        
}
    
    global void execute(SchedulableContext SC) 
{
        SendEmailNotification30Days_CLM batchable = new SendEmailNotification30days_CLM();
        database.executebatch(batchable);
}
}
Raj VakatiRaj Vakati
Try this code and set data accordingly 
 
@isTest
private class SendEmailNotification30days_CLMTest {
    
    static testmethod void test() {        
        Test.startTest();
		
		Account acc = new Account();
        acc.name='Test Account';
        acc.AccountNumber = '26545215';
        insert acc;
        
	    Contact con =  new Contact();
	    con.AccountId = acc.Id;
	    con.LastName = 'Test';
	    insert con;	  
	    
	    Apttus__APTS_Agreement__c agreement =  new Apttus__APTS_Agreement__c();
	   	agreement.Apttus__Account__c = acc.id;
	   	agreement.Name = 'SA';
	  	agreement.Apttus__Company_Signed_By__c = con.id;
	   	insert agreement;	
	   	
	    Apttus_DocuApi__DocuSignEnvelope__c signEnvelope = new Apttus_DocuApi__DocuSignEnvelope__c ();
    	signEnvelope.name = 'Test';
    	signEnvelope.Apttus_DocuApi__EmailSubject__c = '';
    	//signEnvelope.Apttus_CMDSign__Agreement__c = agmt2.id;
    	insert signEnvelope;
    	
    	agreement.Apttus__Agreement_Category__c = 'Multi-Party';
        agreement.Apttus_CMDSign__DocuSignEnvelopeId__c = signEnvelope.id;
        update agreement;
    	
		 Date ed = Date.today().addDays(30);
    	
    	List<Apttus_DocuApi__DocuSignEnvelopeRecipientStatus__c> statusList = new List<Apttus_DocuApi__DocuSignEnvelopeRecipientStatus__c>();
         for(Integer i=1;i<10;i++){            
            Apttus_DocuApi__DocuSignEnvelopeRecipientStatus__c statusRec = 
			new Apttus_DocuApi__DocuSignEnvelopeRecipientStatus__c(Apttus_DocuApi__DocuSignEnvelopeId__c = signEnvelope.id,
                                                                        Apttus_DocuApi__Email__c =  'test@test.com', Apttus_DocuApi__SignedDateTime__c = Date.newInstance(2016, 10, 11),
                                                                        Apttus_DocuApi__Status__c = 'completed',
																		Apttus_DocuApi__RecipientName__c = 'Test Name',
User_Flag_CLM__c  =false ,
Customer_Signed_Date_CLM__c =ed , 																		
																		Apttus_DocuApi__RoutingOrder__c = string.valueOf(i));
           statusList.add(statusRec);
        }
        insert statusList; 	
		
		
		
		
		
        SendEmailNotification30days_CLM  uca = new SendEmailNotification30days_CLM ();
        Id batchId = Database.executeBatch(uca);
        Test.stopTest();
        
    }
    
}

 
GeetuGeetu
thanks Raj, i have made test class as per your inputs given. But execute method is not getting executed in test class. So code coveraege is just 7 . I am not able to increase code coverage although i have created agreement,and other required data pls u pls help
James Potter 5James Potter 5
Thanks for sharing such an amazing code mybkexperience (https://www.mybkexperience.xyz/)