tylerzika
7/20/2017 - 6:53 PM

test schedule batch

test schedule batch

static testmethod void scheduledBatch() {

    String CRON_EXP = '0 0 0 15 3 ? 2022';

    // make the lead trigger not run on this test, so batch code can run.
    // if you insert leads in the test code, they will be converted on insert,
    // because part of the lead trigger is active.
  Run_Lead_Trigger__c setting = [SELECT On__c FROM Run_Lead_Trigger__c];  

  setting.On__c = false;

  update setting;

  Account a = [SELECT Id FROM Account];

  User oldOwner = [SELECT Id FROM User WHERE LastName = 'Owner1'];
  User newOwner = [SELECT Id FROM User WHERE LastName = 'Owner2'];

  // insert 1 contact
  Contact c  	      = new Contact();
  c.AccountId       = a.Id;
  c.LastName        = 'Test LastName';
  c.Email           = 'test@email.com';
  c.Marketo_Sync__c = true;
  c.OwnerId         = oldOwner.Id;

  insert c;

  // insert 2 leads        
  List<Lead> leads = new List<Lead>();

  leads.add(new Lead(LastName                              = 'Test LastName', 
                     Email                                 = 'test@email.com', 
         Subscription_AlumConnect__c           = true,
                     Subscription_EQ_Email__c              = true,
                     Subscription_Gems_Gemology_Email__c   = true,
                     Subscription_GIA_Insider__c           = true, 
                     Subscription_Products_and_Services__c = true, 
                     Subscription_Retailer_Support__c      = true,
                     Marketo_Sync__c                       = true, 
                     OwnerId = newOwner.Id));

  leads.add(new Lead(LastName = 'Test LastName2', Email = 'test@email2.com'));

  insert leads;    

  Test.startTest();
  // Schedule the test job
  String jobId = System.schedule('Schedule LeadConversionBatch',
                                 CRON_EXP, 
                                 new LeadConversionBatchSchedule());         
  // Verify the scheduled job has not run yet.

  System.assertEquals(2, [SELECT count() FROM Lead WHERE IsConverted = false]);
  // Stopping the test will run the job synchronously
  Test.stopTest();

  // Now that the scheduled job has executed,
  // check that our leads were converted
  System.assertEquals(1, [SELECT count() FROM Lead WHERE IsConverted = true]);

}