[SalesForce] Test class for Schedulable class

I have the following class which implements the 'schedulable' interface and need help to write a test class to complete the code coverage please:

the class checks if the campaign start date is current date and if so extracts all the campaigns and sends an email:

  public class CampaignMailOnStartDate implements Schedulable{
    public static void getStartdateToday(List<Campaign> campaign){
    String objectName = 'Campaign';
    String query = 'SELECT';

    //Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();
    Map <String, Schema.SObjectField> objectFields = Campaign.getSobjectType().getDescribe().fields.getMap();
    for (SObjectField fld : objectFields.values()) {
        string fld_str = string.valueOf(fld);
        query += ' ' + fld + ', ';
        /*if (fld_str == 'id'            || fld_str == 'name'      || fld_str == 'parentid'    ||
            fld_str == 'status'      || fld_str == 'startdate' || fld_str == 'enddate'     ||
            fld_str == 'description' || fld_str == 'ownerid'   || fld_str == 'createddate' ||
            fld_str == 'createdbyid')
        query += ' ' + fld + ', ';*/
    }
    // Strip off the last comma if it exists.
    if (query.subString(query.Length()-2,query.Length()) == ', '){
        query = query.subString(0,query.Length()-2);
    }

    // Add FROM statement
    query += ' FROM ' + objectName;
    query += ' WHERE StartDate = TODAY'; 

    try {   
            List<Campaign> campaignList = new List<Campaign>();
            campaignList = database.query(query);
            for(Campaign c : campaignList){
            campaign.add(c);
            }
        } catch (QueryException e){
            //perform exception handling
            system.debug('Error in Query');
        }   
 }

public static void sendMail(List<Campaign> Campaigns, EmailTemplate template ){
    List< Messaging.SingleEmailMessage> emails = new List< Messaging.SingleEmailMessage>();
    for(Campaign camp:campaigns){
        //New instance of a single email message
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        // Set mail template
        mail.setTemplateId(template.Id);
        // Set target id
        mail.setTargetObjectId(camp.CreatedById);
        // Set mail body
        String htmlBody = template.HtmlValue; 
        User createdBy = [select name from user where id =: camp.CreatedById limit 1]; 
        htmlBody = htmlBody.replace('{!Campaign.CreatedBy}', createdBy.Name);
        htmlBody = htmlBody.replace('{!Campaign.Name}', camp.Name);
        htmlBody = htmlBody.replace('{!Campaign.Description}', camp.Description);

        //htmlBody = htmlBody.replace('{!Campaign.StartDate}', camp.StartDate);
        htmlBody = htmlBody.replace('{!Campaign.Status}', camp.Status);
        htmlBody = htmlBody.replace('{!Campaign.Type}', camp.Type);
        //mail.setHtmlBody(htmlBody);
        system.debug('User Id -> '+ camp.CreatedById + 'Campaign Id -> '+ camp.Id);
        mail.setWhatId(camp.Id);
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setSaveAsActivity(false);
        emails.add( mail );
        //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
   }
    Messaging.sendEmail( emails );
}

   public void execute(SchedulableContext ctx){
        List<Campaign> campaign = new List<Campaign>();
        getStartdateToday(campaign);
        for(Campaign camp:campaign){
        system.debug('Campaign -> ' + camp.Name);}
        //EmailTemplate template = [select Subject, HtmlValue, Body from EmailTemplate where DeveloperName = 'Campaign_Due' Limit 1];
        EmailTemplate template = [select Subject, HtmlValue, Body from EmailTemplate where DeveloperName = 'CampaignDueToStart' Limit 1];
        if (template.Id != null){
        system.debug('Template Id -> ' + template.Id);
        sendMail(campaign, template);            
        }
        else
        system.debug('Template not found');
    }
 }

any inputs on this please?

Best Answer

I recommend you check this link. I've included an adaptation of that link's sample code

@isTest
private class TestSchedulableClass {

   // CRON expression: midnight on March 15.
   // Because this is a test, job executes
   // immediately after Test.stopTest().
   public static String CRON_EXP = '0 0 0 15 3 ? 2022';

   static testmethod void test() {
      Test.startTest();

      // Schedule the test job
      String jobId = System.schedule('Test my class',
                        CRON_EXP, 
                        new CampaignMailOnStartDate());

      // Get the information from the CronTrigger API object
      CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, 
         NextFireTime
         FROM CronTrigger WHERE id = :jobId];

      // Verify the expressions are the same
      System.assertEquals(CRON_EXP, 
         ct.CronExpression);

      // Verify the job has not run
      System.assertEquals(0, ct.TimesTriggered);

      // Verify the next time the job will run
      System.assertEquals('2022-03-15 00:00:00', 
         String.valueOf(ct.NextFireTime));

      Test.stopTest();


   }
}
Related Topic