4

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(); 
    }
}
3
  • 3
    you could brake the logic up into smaller testable methods.. you can keep the methods private and use @TestVisible to expose it to the test class only.. Feb 19, 2014 at 20:12
  • I am new at this - but someone just pointed out that I was missing the (seeAllData=true) keyword on the istest, and that is why no data was showing up. Not sure why my test data didn't either. Brought up the code coverage to 47%.
    – user6943
    Feb 19, 2014 at 20:41
  • 2
    using (seeAllData=true) is only advisable if you NEED it, if you can create the objects it the better way to do it since you will have control over the data going though the tests. By using the (seeAllData=true) you are relining on data that might not exist when you deploy to other orgs etc. Feb 20, 2014 at 0:06

1 Answer 1

2

I did some quick anonymous apex testing, and it would appear that your testing event won't match the SOQL query filter criteria that the scheduled class.

Your testing event is getting created 2 days out from the current system date.

Date myDate = Date.today();
Date myDatePlus = myDate.addDays(2);
Event testEvent = new event (activitydate= myDatePlus);

Your SOQL query in the scheduled class is for ActivityDate > NEXT_N_DAYS:2. This won't pickup your testing event. Either change it to ActivityDate >= NEXT_N_DAYS:2 or add a couple more days onto the addDays() call.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .