+ Start a Discussion
karthikeya 2karthikeya 2 

Test Coverage If/Else Trigger 25%

I'm trying to write a test class for the following class, but I'm only covering 25% of the lines. How can I edit it to cover the  if conditions?

class :

global class Wct_OfferPendingforAnalyst implements Database.Batchable<sObject>,Schedulable{

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String strSql = 'SELECT Id,CreatedDate,Name,RecordTypeID,Owner.Email,WCT_Created_Date_Time__c,WCT_Candidate_Email__c,WCT_Team_Mailbox__c,WCT_RMS_ID__c,WCT_Full_Name__c,WCT_status__c,WCT_AgentState_Time__c, WCT_User_Group__c' +
                     ' FROM WCT_Offer__c'+
                     ' where WCT_status__c IN (\'Offer to Be Extended\', \'Draft in Progress\', \'Returned for Revisions\')'+
                     ' AND owner.email != null'+
                     ' AND RecordType.Name IN (\'US Experienced Hire Offer\', \'US Campus Hire Offer\')'; 
        system.debug('query123******'+strSql);
        return database.getQuerylocator(strSql);
        
    }

    global void execute(Database.BatchableContext BC, List<WCT_Offer__c> scope)
    {
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
        List<Messaging.SingleEmailMessage> mailList2 = new List<Messaging.SingleEmailMessage>();
        
        set<string> setRecEmails = new set<string>();
        set<string> setteamEmails = new set<string>();
        map<string,string> RecTeamEmails = new map<string,string>();
        map<string,string> RecTeamEmails2 = new map<string,string>();
        
        Map<String, List<WCT_Offer__c>> recOffers = new Map<String, List<WCT_Offer__c>>(); 
        Map<String, List<WCT_Offer__c>> recOffers2 = new Map<String, List<WCT_Offer__c>>();
        
        Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.WCT_Offer__c.getRecordTypeInfosById();
        Decimal seconds;
        Decimal hrs;
        Decimal min;
        
        
        for(WCT_Offer__c tmp : scope) {
             WCT_Offer__c offerRecord = (WCT_Offer__c) tmp;
             WCT_Offer__c offerRecord2 = (WCT_Offer__c) tmp;
            
             datetime recorddatetime = date.valueof(offerRecord.CreatedDate);
             system.debug('dt**********'+recorddatetime);
             
             seconds = BusinessHours.diff(Label.WCT_Default_Business_Hours_ID, offerRecord.WCT_Created_Date_Time__c, System.now())/ 1000;
             
             system.debug('Label.WCT_Default_Business_Hours_ID**********'+Label.WCT_Default_Business_Hours_ID);
             
             min = Integer.valueOf(seconds / 60);
             hrs = Integer.valueOf(seconds / 3600);
             system.debug('seconds**********'+seconds);
             system.debug('hrs11**********'+hrs);
             system.debug('min**********'+min);
             
             system.debug('recordtype**********'+rtMap.get(offerRecord.RecordTypeId).getName());
             //-------------------1st---------------------------
             
             if(rtMap.get(offerRecord.RecordTypeId).getName() == 'US Experienced Hire Offer' &&  min >=238 && min < 257) { 
                 system.debug('recordtype1**********'+rtMap.get(offerRecord.RecordTypeId).getName());
                setRecEmails.add(offerRecord.Owner.Email);
                RecTeamEmails.put(offerRecord.Owner.Email,offerRecord.WCT_Team_Mailbox__c);
                system.debug('RecTeamEmails:'+RecTeamEmails);
                List<WCT_Offer__c> offerList = recOffers.get(offerRecord.Owner.Email);
                if(offerList == null) {
                    offerList = new List<WCT_Offer__c>();
                } 
                offerList.add(offerRecord);
                recOffers.put(offerRecord.Owner.Email,offerList);
            }
            //-------------------2nd---------------------------
           if(rtMap.get(offerRecord2.RecordTypeId).getName() == 'US Experienced Hire Offer' && min >=358 && min < 377) {   //hrs >= 6 && hrs < 7
                system.debug('alert2**********');
                setRecEmails.add(offerRecord2.Owner.Email);
                RecTeamEmails2.put(offerRecord2.Owner.Email,offerRecord2.WCT_Team_Mailbox__c);
                system.debug('RecTeamEmails2:'+RecTeamEmails2);
                List<WCT_Offer__c> offerList2 = recOffers2.get(offerRecord2.Owner.Email);
                if(offerList2 == null) {
                    offerList2 = new List<WCT_Offer__c>();
                } 
                offerList2.add(offerRecord2);
                recOffers2.put(offerRecord2.Owner.Email,offerList2);
            }
            
        }
        
        
        
        String strEmailTop ='';
        String strEmailBody ='';
        String strEmailBottom ='';
        /*
        strEmailTop  += '<!DOCTYPE html> <html> <head> <style> table,th,td { border: 1px solid black; border-collapse:collapse; }, th,td { padding:5px; } </style> </head> <body> <br> Dear Analyst,<br> <br>';
        strEmailTop  += 'Offer Letter(s) are pending for more than 4 Hours.';
        strEmailTop  += ' <br> <br> <br> <table> <thead> <tr> <th>Offer Name</th> <th>Candidate Name</th> <th>RMS ID</th> <th>Candidate Email</th> <th>Status</th> <th>Offer letter link</th> </tr> </thead> <tbody>';
        
        strEmailBottom += '</tbody> </table> <br> Thank you,<br> Deloitte Recruting.<br> </body> </html>';
        */
        
        for(string strRecEmail: recOffers.keyset()) {
            strEmailBody ='';
            for(WCT_Offer__c offerRecord : recOffers.get(strRecEmail)) {
                
                strEmailTop  += '<!DOCTYPE html> <html> <head> <style> table,th,td { border: 1px solid black; border-collapse:collapse; }, th,td { padding:5px; } </style> </head> <body> <br> Dear Analyst,<br> <br>';
                strEmailTop  += 'Offer Letter(s) are pending for more than 4 Hours.';
                strEmailTop  += ' <br> <br> <br> <table> <thead> <tr> <th>Offer Name</th> <th>Candidate Name</th> <th>RMS ID</th> <th>Candidate Email</th> <th>Status</th> <th>Offer letter link</th> </tr> </thead> <tbody>';
                
                strEmailBody += '<tr> <td>'+offerRecord.Name+'</td> <td>'+offerRecord.WCT_full_Name__c+'</td><td>'+offerRecord.WCT_RMS_ID__c+'</td><td>'+offerRecord.WCT_Candidate_Email__c+'</td>';
                strEmailBody += '<td>'+offerRecord.WCT_status__c+'</td><td><a href='+Label.BaseURL+'/'+offerRecord.id+'>URL</a></td></tr>';
                
                strEmailBottom += '</tbody> </table> <br> Thank you,<br> Deloitte Recruting.<br> </body> </html>';
            }
            list<string> ToEmailAddress = new list<string>();
            List<string> ToCcAddress = new List<string>();
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            ToEmailAddress.add(strRecEmail);
            system.debug('strRecEmail1:'+strRecEmail);
            if(RecTeamEmails != null && RecTeamEmails.get(strRecEmail) != null){
                 ToCcAddress.add(RecTeamEmails.get(strRecEmail));
            }
            system.debug('strRecEmail:'+strRecEmail);
            system.debug('ToCcAddress:'+ToCcAddress);
            
            mail.settoAddresses(ToEmailAddress);
            system.debug('ToEmailAddress:'+ToEmailAddress);
            mail.setccAddresses(ToCcAddress);
            system.debug('ToCcAddress1:'+ToCcAddress);
            mail.setSubject('1st Alert Immediate Action: Offer Letter(s) are pending');
            
            mail.setHTMLBody('<font color="green">'+strEmailTop+'</font>'+'<font color="green">'+strEmailBody+'</font>'+'<font color="green">'+strEmailBottom+'</font>');
            mail.setSaveAsActivity(false);
            mailList.add(mail);  
        
        }
        //------------------2nd------------------------------------------------------------------------------------------------
        for(string strRecEmail2 : recOffers2.keyset()) {
            strEmailBody ='';
            for(WCT_Offer__c offerRecord2 : recOffers2.get(strRecEmail2)) {
                strEmailTop  += '<!DOCTYPE html> <html> <head> <style> table,th,td { border: 1px solid black; border-collapse:collapse; }, th,td { padding:5px; } </style> </head> <body> <br> Dear Analyst,<br> <br>';
                strEmailTop  += 'Offer Letter(s) are pending for more than 6 Hours.';
                strEmailTop  += ' <br> <br> <br> <table> <thead> <tr> <th>Offer Name</th> <th>Candidate Name</th> <th>RMS ID</th> <th>Candidate Email</th> <th>Status</th> <th>Offer letter link</th> </tr> </thead> <tbody>';
                
                strEmailBody += '<tr> <td>'+offerRecord2.Name+'</td> <td>'+offerRecord2.WCT_full_Name__c+'</td><td>'+offerRecord2.WCT_RMS_ID__c+'</td><td>'+offerRecord2.WCT_Candidate_Email__c+'</td>';
                strEmailBody += '<td>'+offerRecord2.WCT_status__c+'</td><td><a href='+Label.BaseURL+'/'+offerRecord2.id+'>URL</a></td></tr>';
                
                strEmailBottom += '</tbody> </table> <br> Thank you,<br> Deloitte Recruting.<br> </body> </html>';
            }
            list<string> ToEmailAddress2 = new list<string>();
            List<string> ToCcAddress2 = new List<string>();
            Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
            ToEmailAddress2.add(strRecEmail2);
            system.debug('strRecEmail2:'+strRecEmail2);
            if(RecTeamEmails2 != null && RecTeamEmails2.get(strRecEmail2) != null){
                 ToCcAddress2.add(RecTeamEmails2.get(strRecEmail2));
            }
            system.debug('strRecEmail2:'+strRecEmail2);
            system.debug('ToCcAddress:'+ToCcAddress2);
            mail2.settoAddresses(ToEmailAddress2);
            system.debug('ToEmailAddress:'+ToEmailAddress2);
                ToCcAddress2.add('pjamalapuram@deloitte.com');
                ToCcAddress2.add('rkomma@deloitte.com');
            mail2.setccAddresses(ToCcAddress2);
            system.debug('ToCcAddress1:'+ToCcAddress2);
            mail2.setSubject('2nd Alert Immediate Action: Offer Letter(s) are pending');
            
            mail2.setHTMLBody('<font color="orange">'+strEmailTop+'</font>'+'<font color="orange">'+strEmailBody+'</font>'+'<font color="orange">'+strEmailBottom+'</font>');
            
            mail2.setSaveAsActivity(false);
            mailList2.add(mail2);  
        }
            Messaging.sendEmail(mailList); 
            Messaging.sendEmail(mailList2);
    }

    global void finish(Database.BatchableContext BC){
    }
}
Test class: 

@isTest
public class Wct_OfferPendingforAnalyst_Test{
    static Id employeeRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Employee').getRecordTypeId();
    static testMethod void Wct_OfferPendingforAnalyst_TestMethod (){
     Profile prof = [select id from profile where name='system Administrator'];
     User usr = new User(alias = 'usr', email='us.name@vmail.com',
                emailencodingkey='UTF-8', lastname='lstname',
                timezonesidkey='America/Los_Angeles',
                languagelocalekey='en_US',
                localesidkey='en_US', profileid = prof.Id,
                username='testuser128@testorg.com');
                insert usr;
    
       Account acc = new Account();
       acc.Name='Test';
       Insert acc;
       system.debug('acc values ----'+acc);
       
       Contact con = new Contact ();
       con.AccountId= acc.Id;
       con.LastName='tec';
       con.Email='user@gmail.com';
       Insert con;
       
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
        List<Messaging.SingleEmailMessage> mailList2 = new List<Messaging.SingleEmailMessage>();
        set<string> setRecEmails = new set<string>();
        set<string> setteamEmails = new set<string>();
        map<string,string> RecTeamEmails = new map<string,string>();
        map<string,string> RecTeamEmails2 = new map<string,string>();
        Map<String, List<WCT_Offer__c>> recOffers = new Map<String, List<WCT_Offer__c>>(); 
        Map<String, List<WCT_Offer__c>> recOffers2 = new Map<String, List<WCT_Offer__c>>();
        Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.WCT_Offer__c.getRecordTypeInfosById();
        
        recordtype rc1 = [SELECT Id FROM RecordType WHERE SObjectType = 'WCT_Offer__c' limit 1];
        WCT_Offer__c oppty = new WCT_Offer__c();
        oppty.recordtypeid = rc1.id;
    
       WCT_Offer__c  objCusWct= new WCT_Offer__c();
        
        objCusWct.RecordTypeId = '01240000000QLiP';
        objCusWct.WCT_status__c = 'Offer to Be Extended';
        objCusWct.WCT_Candidate__c = con.Id;
        objCusWct.WCT_Recruiter__c = con.id;
        objCusWct.WCT_Recruiter__c = con.Id;
        objCusWct.WCT_Team_Mailbox__c = 'test@deloitte.com';
        objCusWct.WCT_Type_of_Hire__c = 'Full time';
        objCusWct.OwnerId = usr.id;
        insert objCusWct;
        objCusWct.recalculateformulas();  
        WCT_Offer__c  ls =   [select id,createddate from WCT_Offer__c  where id =: objCusWct.id limit 1];
         List<BusinessHours> bhs=[select id from BusinessHours where IsDefault=true];     
         Decimal  seconds = BusinessHours.diff(Label.WCT_Default_Business_Hours_ID, objCusWct.WCT_Created_Date_Time__c, System.now())/ 1000;
             
        Decimal       hrs = Integer.valueOf(seconds / 3600);
        Decimal      min = Integer.valueOf(hrs/60);   
        
        Test.StartTest();
        Wct_OfferPendingforAnalyst objBatch = new Wct_OfferPendingforAnalyst();
        ID batchprocessid = Database.executeBatch(objBatch);
        Test.StopTest();
          
    }
}


Thanks in advance
claperclaper
Hi !

It seems to be that on your test class you will need to create test methods with the following codition:
  • For the 1st. if:
    • ensure you insert at least 1 record with record type ="US Experienced Hire Offer"
    • modify the label WCT_Default_Business_Hours_ID and WCT_Created_Date_Time__c, so the calculation of the variable "min" [ min = Integer.valueOf(seconds / 60); ] gives you a value betwen 238 and 237.
  • For the 2nd. if:
    • same as before but,  this time the value of min should be between 358 and  377
karthikeya 2karthikeya 2
Still i am not able to complete it, can any one heple to to complete this.