@isTest
private class LeadUpdateTriggerHandlerTest {
@isTest
static void testHandleUpdate() {
// Create test data
Lead testLead = new Lead(FirstName = 'Test', LastName = 'Lead', Company = 'Test Company');
insert testLead;
// Simulate updated data
Map<String, Object> updatedData = new Map<String, Object>();
updatedData.put('Id', testLead.Id);
updatedData.put('FirstName', 'Updated First Name');
updatedData.put('LastName', 'Updated Last Name');
updatedData.put('Company', 'Updated Company');
List<Map<String, Object>> updatedDataList = new List<Map<String, Object>>();
updatedDataList.add(updatedData);
// Convert the updated data to JSON
String updatedDataJson = JSON.serialize(updatedDataList);
// Create a mock implementation of the HttpCalloutMock interface
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
// Call the handleUpdate method (replace 'LeadUpdateTriggerHandler' with the actual class name)
Test.startTest();
LeadApiTriggerHelper.handleUpdate(updatedDataJson);
Test.stopTest();
// Perform assertions or verifications as needed
// Verify the expected behavior of the handleUpdate method
}
@isTest
static void testProcessLead() {
// Create test data
Lead testLead = new Lead(FirstName = 'Test', LastName = 'Lead', Company = 'Test Company');
insert testLead;
// Simulate updated data
Map<String, Object> updatedData = new Map<String, Object>();
updatedData.put('Id', testLead.Id);
updatedData.put('FirstName', 'Updated First Name');
updatedData.put('LastName', 'Updated Last Name');
updatedData.put('Company', 'Updated Company');
List<Map<String, Object>> updatedDataList = new List<Map<String, Object>>();
updatedDataList.add(updatedData);
// Convert the updated data to JSON
String updatedDataJson = JSON.serialize(updatedDataList);
// Create a mock implementation of the HttpCalloutMock interface
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
// Call the processLead method (replace 'LeadUpdateTriggerHandler' with the actual class name)
Test.startTest();
LeadApiTriggerHelper.processLead(new List<Lead>{testLead}, new Map<Id, Lead>{testLead.Id => testLead});
Test.stopTest();
// Perform assertions or verifications as needed
// Verify the expected behavior of the processLead method
}
@isTest
static void testCreateUpdatedData() {
// Create test data
Lead testLead = new Lead(FirstName = 'Test', LastName = 'Lead', Company = 'Test Company');
// Get the Lead object describe information
Schema.DescribeSObjectResult leadDescribe = Lead.SObjectType.getDescribe();
Map<String, Schema.SObjectField> fieldsMap = leadDescribe.fields.getMap();
// Call the createUpdatedData method (replace 'LeadUpdateTriggerHandler' with the actual class name)
Map<String, Object> updatedData = LeadApiTriggerHelper.createUpdatedData(testLead, fieldsMap);
// Perform assertions or verifications
//System.assertEquals(testLead.FirstName, updatedData.get('FirstName'));
//System.assertEquals(testLead.LastName, updatedData.get('LastName'));
//System.assertEquals(testLead.Company, updatedData.get('Company'));
// Add more assertions for other fields as needed
}
// Helper class for mocking HTTP callout responses
private class MockHttpResponseGenerator implements HttpCalloutMock {
public HttpResponse respond(HttpRequest request){
// Create a mock HTTP response
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('{"success": true}');
response.setStatusCode(200);
return response;
}
}
@isTest
static void testHandleAfterInsert() {
// Create test data
Lead testLead1 = new Lead(FirstName = 'Test', LastName = 'Lead1', Company = 'Test Company', Flag__c = true);
Lead testLead2 = new Lead(FirstName = 'Test', LastName = 'Lead2', Company = 'Test Company', Flag__c = false);
List<Lead> newLeads = new List<Lead>{testLead1, testLead2};
insert newLeads;
// Call the handleAfterInsert method
Test.startTest();
LeadConversionTriggerHandler.handleAfterInsert(newLeads);
Test.stopTest();
// Perform assertions or verifications as needed
// Verify the expected behavior after inserting leads
}
@isTest
static void testHandleAfterUpdate() {
// Create test data
Lead testLead1 = new Lead(FirstName = 'Test', LastName = 'Lead1', Company = 'Test Company', Flag__c = true);
Lead testLead2 = new Lead(FirstName = 'Test', LastName = 'Lead2', Company = 'Test Company', Flag__c = false);
List<Lead> newLeads = new List<Lead>{testLead1, testLead2};
insert newLeads;
testLead1.Flag__c = false;
testLead2.Flag__c = true;
update newLeads;
// Call the handleAfterUpdate method
Test.startTest();
LeadConversionTriggerHandler.handleAfterUpdate(newLeads, new Map<Id, Lead>{testLead1.Id => testLead1, testLead2.Id => testLead2});
Test.stopTest();
// Perform assertions or verifications as needed
// Verify the expected behavior after updating leads
}
// Add more test methods for other handler methods and scenarios as needed
}
Methods defined as TestMethod do not support Web service callouts
In the First Methods I am sending JSON data by HTTP Callouts for Lead when a lead is created or Updated and in Last two methods I am converting lead when it's coming through API when the Flag__c is true
LeadConversionTriggerHandler.handleAfterUpdate
do any callouts?