You need to sign in to do that
Don't have an account?
Dustin Mayfield
Code Coverage Help - Sending Email Class
I created a class that I'd like to schedule with apex jobs to run on certain days. The class is working as intended, but i'm struggling with the test class. I'm not sure how I'm supposed to cover the email portion of the class:
Any suggestions on how i can increase the code coverage? Thanks so much!
public with sharing class TimesheetReminder { public static void TimesheetReminder() { //Get list of active employees List <sabersolutions__Employee__c> listOfRoles = [SELECT id, sabersolutions__Work_Email__c, sabersolutions__First_Name__c, Name FROM sabersolutions__Employee__c WHERE Status__c = 'Active']; List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); for (sabersolutions__Employee__c role : listOfRoles) { Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {role.sabersolutions__Work_Email__c}; // String[] ccAddresses = new String[] {'hr@company.com'}; // email.setccAddresses(ccAddresses); email.setToAddresses(toAddresses); email.SetReplyTo('hr@company.com'); email.setSenderDisplayName('Human Resources - Timekeeping Reminder'); email.setBccSender(false); email.setUseSignature(false); email.SetSubject('Timesheet Reminder || Reminder to Complete'); email.setHtmlBody('Hello '+role.sabersolutions__First_Name__c+' '+role.Name+',<br><br>This is a friendly reminder to please complete your timesheet before close of business today.'); mails.add(email); } Messaging.sendEmail(mails); } }This is the test class I have so far, however it only covers 33% of the class...
@isTest public class TimesheetReminderTest { @istest public static void Test1() { sabersolutions__Employee__c emp = new sabersolutions__Employee__c(); emp.sabersolutions__First_Name__c = 'TestFirstName'; emp.Name = 'TestLastName'; emp.sabersolutions__Work_Email__c = 'test@test.com'; insert emp; Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {emp.sabersolutions__Work_Email__c}; email.setToAddresses(toAddresses); email.SetReplyTo('hr@company.com'); email.setSenderDisplayName('Human Resources - Timekeeping Reminder'); email.setBccSender(false); email.setUseSignature(false); email.SetSubject('Timesheet Reminder || Reminder to Complete'); email.setHtmlBody('Hello '+emp.sabersolutions__First_Name__c+' '+emp.Name+',<br><br>This is a friendly reminder to please complete your timesheet before close of business today.'); TimesheetReminder.TimesheetReminder(); Integer emailInvocations = Limits.getEmailInvocations(); system.assertEquals(1, emailInvocations, 'An email should be sent'); } }
Any suggestions on how i can increase the code coverage? Thanks so much!
Raj Vakati
Use this and add other fields while inserting the data