I have code that I would like to test and get 100%. When I run the test ((27% code coverage) the code that does not get processed is from - for (event eWO : eventWorkOrderList) {
But when I run the query in debug mode I get results (a few records) so I know that the list is not empty. Why doesn't the test go through the code?
Class:
global class sndWorkOrderConfEmailDailyScheduler implements Schedulable {
global void execute(SchedulableContext ctx) {
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
FROM CronTrigger WHERE Id = :ctx.getTriggerId()];
System.debug(ct.CronExpression);
System.debug(ct.TimesTriggered);
integer noOfDaysPrior = 2;
//MG Get list of Events that are WorkOrders and their Due dates are in 2 days (noOfDaysPrior)
string strPlainTextBody;
datetime dtActivityDate;
string strActivityDate;
string strFacilityLocation;
string strMedWasteEmail = '[email protected]';
string strMedWastePhone = '888.431.6386 x 2';
string strMedWasteSignature = 'MedWaste Management' + ' \r'
+ strMedWastePhone + ' Fax:718.725.7037' + ' \r'
+ 'email: '+ strMedWasteEmail + ' \r';
List<Event> eventWorkOrdersList= new List<Event>();
eventWorkOrdersList = [SELECT id, Whatid, whoid, StartDateTime,
ActivityDate, IsRecurrence, RecurrenceStartDateTime,
Subject, Ownerid, Business_Hours__c, Notes__c,
Service_City__c, Service_State__c, ServiceAddress__c,
Service_Zip__c from Event
where IsRecurrence = false and
ActivityDate > NEXT_N_DAYS:2 AND
ActivityDate <= NEXT_N_DAYS:10 AND
Subject Like 'MWP%'
Order by ActivityDate LIMIT 180];
// AND ActivityDate < NEXT_N_DAYS:3 LIMIT 20]; //doesn't work unless it's <=.
for (event eWO : eventWorkOrdersList) {
system.debug('*MGMGMG* ' + eWO.Subject );
dtActivityDate = eWO.activitydate;
strActivityDate = dtActivityDate.format('EEE, MMM d, yyyy');
strFacilityLocation = eWO.ServiceAddress__c + ', ' + eWO.Service_City__c + ', ' + eWO.Service_State__c + ' ' + eWO.Service_Zip__c;
if (eWo.Subject.contains('MWP')) {
//////Rest of code here
} // end EWO
}//end for
}// class
}
The Test Class is:
@istest
private class Test2SchedulableClass {
private static testmethod void test2sndWorkOrderConfEmail() {
Date myDate = Date.today();
Date myDatePlus = myDate.addDays(2);
Datetime myDateTime = DateTime.now();
Datetime myDateTimePlus = myDateTime.addDays(2);
Event testEvent = new event (startdatetime = myDateTimePlus, activitydate= myDatePlus, subject='MWP Test Pickup',
business_hours__c = 'm:9-5', notes__c = 'Testing work order email',
Service_City__c = 'Concordia', Service_State__c = 'AL',
ServiceAddress__c = '123 Concord Lane', Service_Zip__c = '99999');
//Insert Event
insert testevent;
Event testevent2 = [select startdatetime, activitydate, subject,
business_hours__c , notes__c ,
Service_City__c , Service_State__c,
ServiceAddress__c , Service_Zip__c from event where id = :testevent.Id];
test.startTest();
sndWorkOrderConfEmailDailyScheduler sh1 = new sndWorkOrderConfEmailDailyScheduler();
String sch = '0 0 23 * * ?'; system.schedule('Test Work Order Conf', sch, sh1); Test.stopTest();
}
}