+ Start a Discussion
Sushma  RamakrishnanSushma Ramakrishnan 

Test Help to cover Code Coverage of Test Class

Hi All,

The below lines in my Apex Class are not getting covered :
 
global class TestIncidentsEmailProcess implements Schedulable{

   global void execute(SchedulableContext SC){
    try
        {      
         emailProcessing();
        }
         catch(Exception ex)
        {
            System.debug('Exception in method email process: '+ex.getStackTraceString()+':'+ex.getMessage());
        }    
   }   
   public static void emailProcessing(){
   try{
    Set<String> incidentID = new Set<String>();
    List<IncidentsEmail__c> emailRecordsToUpdate = new List<IncidentsEmail__c>();
    Date todaysDate = Date.today();
    List<IncidentsEmail__c> incidentsCreatedOrUpdatedList = [select id,Name,IncidentCreatedDate__c,EmailStatus__c from IncidentsEmail__c where IncidentCreatedDate__c =: todaysDate and EmailStatus__c!='Sent'];
    for(IncidentsEmail__c incidentsEmail :incidentsCreatedOrUpdatedList){

        incidentID.add(incidentsEmail.Name);     
    }
    List<Company_Incidents__c> incList = [select id, Name,company__r.name,company__r.Test_Manager__r.email, Test_Manager_Link_f__c,title__c,Incident__c,Incident_Id__c,Impact__c,Email_Trigger__c,Incident_Formula__c,Open_Date__c from Company_Incidents__c where Id in:incidentID and Email_Trigger__c!='Updated' order by company__r.Test_Manager__r.email,company__r.name,Open_Date__c];
       Map<String, List<Company_Incidents__c>> result = new Map<String, List<Company_Incidents__c>>();
    for (Company_Incidents__c p : incList) {
      String email = p.company__r.Supplier_Manager__r.email;
      if(null != email){
          List<Company_Incidents__c> list1 = result.get(email);
          if (list1 == null) {
              list1 = new List<Company_Incidents__c>();
              result.put(email, list1);
          }
          list1.add(p);
      }
    }         
         for(String emailKey : result.keySet()){
           List<Company_Incidents__c> childList = result.get(emailKey);
          if(childList != NULL && childList.size() > 0){
            List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
            String Content = AutoNotification.IntimateSMO(childList);
            mail.setHtmlBody(Content);
            mail.setPlainTextBody(Content);
            mail.setSaveAsActivity(false);
            mail.setSubject('SupplierForce Incident Notification');
            for(OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress]) {
                if(owa.DisplayName.contains('ERM Salesforce Compliance')){
                    mail.setOrgWideEmailAddressId(owa.id); 
                }
            }            
            List<String> toAddress = new List<String>();
             toAddress.add(emailKey);
            mail.setToAddresses(toAddress);
            mails.add(mail);
            Messaging.SendEmailResult[] resultMail =    Messaging.sendEmail(mails, false);
           }
       }        
       List<IncidentsEmail__c> incidentsCreatedOrUpdatedList1 = [select id,Name,IncidentCreatedDate__c,EmailStatus__c from IncidentsEmail__c where IncidentCreatedDate__c =: todaysDate and EmailStatus__c!='Sent'];
       for(IncidentsEmail__c incidentsEmail :incidentsCreatedOrUpdatedList1){
        incidentsEmail.EmailStatus__c = 'Sent';
        emailRecordsToUpdate.add(incidentsEmail);
       }
       System.debug('emailRecordsToUpdate::'+emailRecordsToUpdate);
       update emailRecordsToUpdate;
    }catch(Exception e){
        String recipientAddress = 'suneetha.konda@fmr.com';
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String textbody = 'There has been an error while processing the service manager job from SMIncidents class';
        String textSubject = 'Job error';
        String[] toAddresses = new String[] {recipientAddress};
        mail.setOrgWideEmailAddressId('xxxxxxxxxxxxxxx');
        mail.setToAddresses(toAddresses);
        mail.setSubject(textSubject);        
        mail.setPlainTextBody(textbody);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
    }
   
    }
 }
The lines in bold are not getting covered.Have achieved 39% and need to make it 75%.

Thanks for any help in Advance!
 
amahmamahm
Hi, i guess you need to tell which lines - because you cannot make code lines bold.
amahmamahm
Hi, have a look here https://developer.salesforce.com/forums/?id=906F00000008wH2IAI and here http://blog.jeffdouglas.com/2008/10/15/how-to-unit-test-sending-mail-in-apex/
Muzammil BajariaMuzammil Bajaria
http://salesforce.stackexchange.com/questions/54587/how-do-i-test-an-apex-trigger-that-sends-an-email

this link might help.
Nisha WarrierNisha Warrier
Hi,
Please mention the line numbers ...!!
Sushma  RamakrishnanSushma Ramakrishnan
Hi All,

The below lines are not getting covered :

incidentID.add(incidentsEmail.Name);

String email = p.company__r.Test_Manager__r.email;      
      if(null != email){
          List<Company_Incidents__c> list1 = result.get(email);
          if (list1 == null) {
              list1 = new List<Company_Incidents__c>();
              result.put(email, list1);
          }
          list1.add(p);

List<Company_Incidents__c> childList = result.get(emailKey);
          if(childList != NULL && childList.size() > 0){

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
            String Content = AutoNotification.IntimateSMO(childList);
            mail.setHtmlBody(Content);
            mail.setPlainTextBody(Content);
            mail.setSaveAsActivity(false);
            mail.setSubject('Test Incident Notification');

for(OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress]) {
                if(owa.DisplayName.contains('Test Salesforce Compliance')){
                    mail.setOrgWideEmailAddressId(owa.id);

List<String> toAddress = new List<String>();
             toAddress.add(emailKey);
List<String> toAddress = new List<String>();
             toAddress.add(emailKey);
 Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(mails, false);

incidentsEmail.EmailStatus__c = 'Sent';
        emailRecordsToUpdate.add(incidentsEmail);

catch(Exception e){
        String recipientAddress = 'Test@test.com';
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String textbody = 'There has been an error while processing the service manager job from TestIncidents class';
        String textSubject = 'Job error';
        String[] toAddresses = new String[] {recipientAddress};
        mail.setOrgWideEmailAddressId('xxxxxxxxxxxxxxx');
        mail.setToAddresses(toAddresses);
        mail.setSubject(textSubject);        
        mail.setPlainTextBody(textbody);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });