function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vserv CapitalVserv Capital 

I am write class leadhelper, how can write test class code

public class LeadHelper {
        public static void createContact(List<Lead> leadList){
        Id ContactRecordTypeId =  Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('New').getRecordTypeId();
        List<Contact> listCo=new List<Contact>();
        for(Lead l : leadList){
            Contact Con = new Contact();
            Con.firstName = l.firstName;
            Con.LastName = l.lastname;
            Con.Email = l.Email;
            Con.MobilePhone = l.MobilePhone.Substring(2,l.MobilePhone.length());
            Con.RecordTypeId = ContactRecordTypeId;
            Con.Source__c = 'Facebook';
            Con.Project__c = l.Project__c;
            Con.Address__c = l.Billing_City__c;
            Con.Ad_Name__c = l.Ad_Name__c;
            listCo.add(Con);
}  
     system.debug('listCo : ' + listCo);
        insert listCo;

            
      }
}


Please help me how can write test class code
AnudeepAnudeep (Salesforce Developers) 
You can use the following sample code to get started
 
@isTest
private class LeadHelperTest {
    @isTest static void LeadHelperTest() {

        List<Lead> lstLead =   new List<Lead>{
                          new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open'),
                          new Lead(Company = 'Nike', LastName = 'John', Status = 'Open'),
                          new Lead(Company = 'Miles', LastName = 'Davis', Status = 'Open'),
                          new Lead(Company = 'Reebok', LastName = 'Hillen', Status = 'Open'),
                          new Lead(Company = 'Addidas', LastName = 'Shrin', Status = 'Open')
                         };  
        insert lstLead;

        LeadHelper lH = new LeadHelper();
        lH.createContact(lstLead);
    }    
      
}

Let me know if it gives you any coverage

Anudeep
 
Vserv CapitalVserv Capital
Hi Anudeep

code give erorr
Static method cannot be referenced from a non static context: void LeadHelper.createContact(List<Lead>)
Miranda L 2Miranda L 2
Hi Capital,
you can write test class like this 
@isTest
public class LeadHelperTest {
        static testmethod void TestLeadHelperMethod(){
   
            Lead ld = new Lead(Status ='Working - Contacted',firstName='serv1',LastName='Capital1',Billing_City__c='Mumbai1',Email='serv@capital.com1',MobilePhone='96367866791',project__c='Internal1',ad_Name__c='Mahabodhi1',Company='Mercedes');
            
            list<Lead> ldd = new list<Lead>();
            ldd =[select id,firstName,LastName,Status,Company,Billing_City__c,Email,MobilePhone,project__c,ad_Name__c from Lead];
            LeadHelper.createContact(ldd);

            contact con = new Contact();
            Con.firstName = 'Serv';
            Con.LastName = 'Capital'; 
            Con.Email    = 'serv@capital.com';
            Con.MobilePhone =ld.MobilePhone.Substring(2,ld.MobilePhone.length());
            Con.source__c  = 'facebook';
            Con.project__c = 'Internal';
            Con.Address__c = '123 London';
            Con.ad_Name__c = 'Alaska';
            insert con;
             Con.source__c  = 'Twitter';
            update con;
            
      }

    }

Thanks
AnudeepAnudeep (Salesforce Developers) 
Hi Vserv - To avoid the error 'Satic method cannot be referenced from a non static context: void LeadHelper.createContact(List<Lead>)' 

Replace
 
LeadHelper lH = new LeadHelper();
        lH.createContact(lstLead);

with 
 
LeadHelper.createContact(lstLead);