I've set up a schedulable class that calls a batch class, but when I run the test class, it successfully calls the schedulable class, but the batch class is never called.
What am I missing?
Here is the test class:
@isTest
public class DDParticipantCampaignTest {
public static String CRON_EXP = '0 0 0 15 3 ? 2022';
static testMethod void testParticipant(){
Id DDEventCampaignRecordTypeId = Schema.SObjectType.Campaign.getRecordTypeInfosByName().get('DD Events') == null ? null : Schema.SObjectType.Campaign.getRecordTypeInfosByName().get('DD Events').getRecordTypeId();
Id DDDonationRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('DD Donations') == null ? null : Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('DD Donations').getRecordTypeId();
Datetime yesterday = Datetime.now().addDays(-1);
Campaign c = TestHarness.createCampaign(date.today(), 0);
c.RecordTypeId = DDEventCampaignRecordTypeId;
c.Name = 'Test DD Campaign';
insert c;
Test.setCreatedDate(c.Id, yesterday);
update c;
Contact p = new Contact(FirstName = 'Test', LastName = 'Participant', Email = 'test@participant.com');
p.DonorDrive_ID_walklikemadd__c = '123456';
insert p;
Test.setCreatedDate(p.Id, yesterday);
update p;
Contact d = TestHarness.createContact('Donor');
d.FirstName = 'Test';
d.DonorDrive_ID_walklikemadd__c = '12345';
insert d;
Test.setCreatedDate(d.Id, yesterday);
update d;
Opportunity o = TestHarness.createOpportunity(DDDonationRecordTypeId, c, d);
o.ddrive__Participant_First_Name__c = p.FirstName;
o.ddrive__Participant_Last_Name__c = p.LastName;
o.ddrive__Participant_Email__c = p.Email;
o.StageName = 'Closed Won';
o.CloseDate = date.today().addDays(-1);
insert o;
Test.setCreatedDate(o.Id, yesterday);
update o;
List<Opportunity> opps = [SELECT createdDate FROM Opportunity WHERE Id =: o.Id];
System.debug('Opportunity name: ' + o.Name);
System.debug('Participant name: ' + o.ddrive__Participant_First_Name__c + ' ' + o.ddrive__Participant_Last_Name__c);
System.debug('Opportunity last modified: ' + o.LastModifiedDate);
System.debug('Opportunity created: ' + opps[0].CreatedDate);
Test.startTest();
String jobId = System.schedule('ScheduledApexTest',
CRON_EXP,
new DDParticipantCampaignSched());
Test.stopTest();
List <CampaignMember> camp = new List <CampaignMember>();
String contactLastName;
String campaignName;
For(CampaignMember cm : [SELECT Id FROM CampaignMember]){
contactLastName = cm.Contact.LastName;
campaignName = cm.Campaign.Name;
if(contactLastName == 'Participant' && campaignName == 'Test DD Campaign'){
camp.add(cm);
}
}
System.assertEquals(camp.size(), 1);
}
}
Here is the Schedulable class, which the test is calling successfully:
global class DDParticipantCampaignSched implements Schedulable{
global void execute(SchedulableContext sc) {
DDParticipantCampaignBatch part = new DDParticipantCampaignBatch();
database.executeBatch(part,50);
}
}
And here is the batch class, which is not being called:
global class DDParticipantCampaignBatch implements Database.Batchable<sObject> {
global database.Querylocator start(Database.BatchableContext bc) {
String query = 'SELECT Id '+
' FROM Opportunities'+
' WHERE (ddrive__Participant_First_Name__c != null'+
' AND ddrive__Participant_Last_Name__c != null'+
' AND CampaignId != null';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Opportunity> scope) {
if(scope.size() > 0){
DDParticipantCampaign.createCampaignMembers(scope);
}
}
global void finish(Database.BatchableContext bc) {
}
}