function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dustin MayfieldDustin 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:
 
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 VakatiRaj Vakati
Use this and add other fields while inserting the data
 
@isTest
public class TimesheetReminderTest{

    static testMethod void sendTimeSheet() {
    
    Test.startTest();
         
       Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
       System.debug('What is the profile id ' + profile1);
       UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
       date tDate = date.today();
       date uDate = Date.today().addDays(30);
        
         User u = new User(
            UserRoleId = portalRole.Id,
            ProfileId = profile1.Id,
            Username = 'xyz@sdasdasdasd.com',
            Alias = 'batman',
            Email='xyz@kapladsasdasdan.com',
            EmailEncodingKey='UTF-8',
            Firstname='Bruce',
            Lastname='Wayne',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
            insert u;
        
       
        System.runas(u) {
			
			
			sabersolutions__Employee__c ins = new sabersolutions__Employee__c();
			ins.Status__c = 'Active';
			ins.sabersolutions__Work_Email__c='test@shdfjhsdf.com';
			ins.sabersolutions__First_Name__c='demo';
			ins.Name ='Test';
			//add other fields 
			insert ins ;
			TimesheetReminder.TimesheetReminder()
			
		}
        
           
                
    Test.stopTest();  
     }
   } 
 }