I have below schedulable Apex class which creates a platform event record if a Lead is in open status for more than 5 mins. I am not able to cover the test class for the platform events.
Can anyone please suggest what should be done, or where am I going wrong?
public class LeadStatusUpdateScheduled implements Schedulable {
@testVisible private List<Lead_Outbound__e> leadOutboundList = new List<Lead_Outbound__e>();
@testVisible private List<Database.SaveResult> saveResults = new List<Database.SaveResult>();
List<Lead> leadsToUpdateWithNewStatus = new List<Lead>();
public void execute(SchedulableContext sc) {
List<Lead> leadsToUpdate = [Select Id, FirstName, LastName, Status, Phone, MobilePhone,
Email, Street, City, PostalCode, StateCode, CountryCode, LastModifiedDate,
Dealership__r.RCY_ExternalAccountID__c, Origin__c, RCY_InterestType__c,
Source__c, Brand__c, InterestCar__c, RCY_VIN__c, RCY_AcademicTitle__c, Stock_ID__c,
ID_Passport_No__c, Promotion_Communication__c, Consent_Date__c
FROM Lead
WHERE LastModifiedDate <= :DateTime.now().addMinutes(-5)
AND Status = 'E0001'];
for (Lead leadrec : leadsToUpdate) {
//Lead Outbound Record creation
Lead_Outbound__e le = new Lead_Outbound__e();
le.Academic_Title__c = leadrec.RCY_AcademicTitle__c;
le.Brand__c = leadrec.Brand__c;
le.First_Name__c = leadrec.FirstName;
le.Last_Name__c = leadrec.LastName;
le.Phone__c = leadrec.Phone;
le.Lead_Origin__c = leadrec.Origin__c;
le.Mobile__c = leadrec.MobilePhone;
le.VIN__c = leadrec.RCY_VIN__c;
le.Lead_Status__c = leadrec.Status;
le.Country__c = leadrec.CountryCode;
le.State__c = leadrec.StateCode;
le.City__c = leadrec.City;
le.Street__c = leadrec.Street;
le.Postal_Code__c = leadrec.PostalCode;
le.Email__c = leadrec.Email;
le.Dealer__c = leadrec.Dealership__r.RCY_ExternalAccountID__c;
le.Lead_Type__c = leadrec.RCY_InterestType__c;
le.Lead_Source__c = leadrec.Source__c;
le.Model_Code__c = leadrec.InterestCar__c;
le.Stock_ID__c = leadrec.Stock_ID__c;
le.ID_Passport_No__c = leadrec.ID_Passport_No__c;
le.Consent_Date__c = leadrec.Consent_Date__c;
le.Promotional_Communication__c = leadrec.Promotion_Communication__c;
leadOutboundList.add(le);
//Lead field update
leadrec.Status = 'In Routing';
leadsToUpdateWithNewStatus.add(leadrec);
}
//Publishing Platform Events
if (!leadOutboundList.isEmpty() && leadOutboundList.size() > 0) {
saveResults = Eventbus.publish(leadOutboundList);
}
//Updating Lead Object Records
if (!leadsToUpdateWithNewStatus.isEmpty()) {
update leadsToUpdateWithNewStatus;
}
}
}
and the test class:
@isTest
private class LeadStatusUpdateScheduledTest {
@isTest
static void testLeadStatusUpdateScheduled() {
// Create test leads
Id dealerRecordType = [SELECT Id FROM RecordType WHERE Name = 'Dealer Account' LIMIT 1].Id;
Account dealer1 = new Account(RecordTypeId = dealerRecordType, RCY_AccountEmail__c = '[email protected]', Name = 'Test Dealer Account',
Phone = '1234567890', Type = 'Dealer', RCY_ExternalAccountID__c = '0000001711');
insert dealer1;
List<Lead> leadList = new List<Lead>();
Lead l = new Lead();
l.FirstName = 'Test';
l.LastName = 'User';
l.Status = 'E0001';
l.Phone = '1234567890';
l.MobilePhone = '1234567890';
l.Email = '[email protected]';
l.Street = 'Test Street';
l.City = 'Frankfurt';
l.PostalCode = '67423';
l.CountryCode = 'DE';
l.Dealership__c = dealer1.Id;
l.Origin__c = '008';
l.RCY_InterestType__c = 'Dealer Request';
l.Source__c = 'AEV';
l.Brand__c = 'Volkswagen';
l.RCY_VIN__c = 'AW309124356';
l.RCY_AcademicTitle__c = 'DR';
l.Stock_ID__c = '209345';
l.ID_Passport_No__c = '851206779845';
l.Promotion_Communication__c = TRUE;
l.Consent_Date__c = Date.valueOf('2022-08-16');
leadList.add(l);
insert leadList;
Lead_Outbound__e le = new Lead_Outbound__e();
le.Academic_Title__c = l.RCY_AcademicTitle__c;
le.Brand__c = l.Brand__c;
le.First_Name__c = l.FirstName;
le.Last_Name__c = l.LastName;
le.Phone__c = l.Phone;
le.Lead_Origin__c = l.Origin__c;
le.Mobile__c = l.MobilePhone;
le.VIN__c = l.RCY_VIN__c;
le.Lead_Status__c = l.Status;
le.Country__c = l.CountryCode;
le.City__c = l.City;
le.Street__c = l.Street;
le.Postal_Code__c = l.PostalCode;
le.Email__c = l.Email;
le.Dealer__c = l.Dealership__r.RCY_ExternalAccountID__c;
le.Lead_Type__c = l.RCY_InterestType__c;
le.Lead_Source__c = l.Source__c;
le.Stock_ID__c = l.Stock_ID__c;
le.ID_Passport_No__c = l.ID_Passport_No__c;
le.Consent_Date__c = l.Consent_Date__c;
le.Promotional_Communication__c = l.Promotion_Communication__c;
List<Lead_Outbound__e> leadOutList = new List<Lead_Outbound__e>();
leadOutList.add(le);
DateTime createdDate = DateTime.Now().addMinutes(-5);
Test.setCreatedDate(l.Id, createdDate);
Test.startTest();
String scheduleTime = '0 0 * * * ?';
LeadStatusUpdateScheduled scheduledJob = new LeadStatusUpdateScheduled();
String jobId = System.schedule('Test Lead Status Update Job', scheduleTime, scheduledJob);
List<Database.SaveResult> sr = EventBus.publish(leadOutList);
Test.getEventBus().deliver();
System.Test.stopTest();
Lead lead = [SELECT Id, Status FROM Lead WHERE Name = 'Test User'];
System.assertEquals('In Routing', lead.Status);
System.assertEquals(true, sr[0].isSuccess());
}
}
I tried to use EventBus.publish() and Test.getEventBus().deliver() thinking firstly publishing then delivering the event and then checking in the event published successfully. But this block is not covering the platform event.
Had to refactor the code little bit to pull the PE record out of the execute() method in a different public method and call it from within the execute() method. Later, called the public method from test class to create PE record which provided 90% coverage.
The updated schedulable class:
And the updated test class:
}