You need to sign in to do that
Don't have an account?

Can Some one help me on writing test class for the apex class
Hi,
here is my apex class where I am updating county field on lead with name of custom county object based on postal code.
Apex Class:
public class LeadTriggerHandler {
public LeadTriggerHandler(ApexPages.StandardController controller){
}
public static void beforeLeadInsertHandler(List<Lead> newLeadList){
List<Lead> updatedLeads = new List<Lead> ();
for(Lead newLead:newLeadList){
if(newLead.PostalCode!=NULL){
updatedLeads.add(newLead);
}
}
if(!updatedLeads.isEmpty()){
updateLeadCountyOnLead(updatedLeads);
}
}
public static void beforeLeadUpdateHandler(Map<Id, Lead> oldLeadMap, List<Lead> newLeadList){
List<Lead> updatedLeads = new List<Lead> ();
for(Lead newLead:newLeadList){
Lead oldLead = oldLeadMap.get(newLead.Id);
if(newLead.PostalCode!=NULL&&newLead.PostalCode!=''&&newLead.PostalCode!=oldLead.PostalCode){
updatedLeads.add(newLead);
}
}
if(!updatedLeads.isEmpty()){
updateLeadCountyOnLead(updatedLeads);
}
}
private static void updateLeadCountyOnLead(List<Lead> leadData){
set<string> postalCodeSet = new set<string> ();
for(Lead lds:LeadData){
if(lds.PostalCode!=NULL){
postalCodeSet.add(lds.PostalCode);
}
}
Map<String, String> postalCodeCountyIDMap = new Map<String, String>();
if(!postalCodeSet.isEmpty()){
for(County__c county:[SELECT ID,Name,Postal_Code__c
FROM County__c
WHERE Postal_Code__c IN:postalCodeSet ])
{
postalCodeCountyIDMap.put(county.Postal_Code__c, county.name);
}
system.debug('PostalcountyMap::'+postalCodeCountyIDMap);
for(Lead lds:LeadData){
if(!postalCodeCountyIDMap.isEmpty() && postalCodeCountyIDMap.containsKey(lds.PostalCode)){
lds.County__c= postalCodeCountyIDMap.get(lds.PostalCode);
} else {
lds.County__c='';
}
}
}
}
}
This is the test data factory :
@isTest
public class TestDataFactory {
public static list<Lead> createLeads(Integer numLeads)
{
list<Lead> leads = new list<Lead>();
for(Integer i=0;i<numLeads;i++) {
Lead l = new Lead(LastName='TestLead' + i);
leads.add(l);
}
insert leads;
return leads;
}
}
I am very new to development. Please help me on writing test class for the above apex class
here is my apex class where I am updating county field on lead with name of custom county object based on postal code.
Apex Class:
public class LeadTriggerHandler {
public LeadTriggerHandler(ApexPages.StandardController controller){
}
public static void beforeLeadInsertHandler(List<Lead> newLeadList){
List<Lead> updatedLeads = new List<Lead> ();
for(Lead newLead:newLeadList){
if(newLead.PostalCode!=NULL){
updatedLeads.add(newLead);
}
}
if(!updatedLeads.isEmpty()){
updateLeadCountyOnLead(updatedLeads);
}
}
public static void beforeLeadUpdateHandler(Map<Id, Lead> oldLeadMap, List<Lead> newLeadList){
List<Lead> updatedLeads = new List<Lead> ();
for(Lead newLead:newLeadList){
Lead oldLead = oldLeadMap.get(newLead.Id);
if(newLead.PostalCode!=NULL&&newLead.PostalCode!=''&&newLead.PostalCode!=oldLead.PostalCode){
updatedLeads.add(newLead);
}
}
if(!updatedLeads.isEmpty()){
updateLeadCountyOnLead(updatedLeads);
}
}
private static void updateLeadCountyOnLead(List<Lead> leadData){
set<string> postalCodeSet = new set<string> ();
for(Lead lds:LeadData){
if(lds.PostalCode!=NULL){
postalCodeSet.add(lds.PostalCode);
}
}
Map<String, String> postalCodeCountyIDMap = new Map<String, String>();
if(!postalCodeSet.isEmpty()){
for(County__c county:[SELECT ID,Name,Postal_Code__c
FROM County__c
WHERE Postal_Code__c IN:postalCodeSet ])
{
postalCodeCountyIDMap.put(county.Postal_Code__c, county.name);
}
system.debug('PostalcountyMap::'+postalCodeCountyIDMap);
for(Lead lds:LeadData){
if(!postalCodeCountyIDMap.isEmpty() && postalCodeCountyIDMap.containsKey(lds.PostalCode)){
lds.County__c= postalCodeCountyIDMap.get(lds.PostalCode);
} else {
lds.County__c='';
}
}
}
}
}
This is the test data factory :
@isTest
public class TestDataFactory {
public static list<Lead> createLeads(Integer numLeads)
{
list<Lead> leads = new list<Lead>();
for(Integer i=0;i<numLeads;i++) {
Lead l = new Lead(LastName='TestLead' + i);
leads.add(l);
}
insert leads;
return leads;
}
}
I am very new to development. Please help me on writing test class for the above apex class