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
riazriaz 

can any one help me how tot test this code,

// Creates an Opportunity record when a Lead record is converted
public class MyLead {
    public static void convertLead(Lead[] leads) {
      Database.LeadConvert leadConvert = new Database.LeadConvert();
      for (Lead lead :leads) {
        if (!(lead.IsConverted)) {
          // Check if an account exists with email. Attach Contact and Account, convert Lead.
          Contact[] myContacts = [SELECT Id, AccountId FROM Contact WHERE Email = :lead.Email Limit 1];
          if (myContacts.size() == 1) {
            System.debug('Existing Contact');
            MyLead.attachExistingConvert(lead, myContacts[0]);
          }
          else {
            System.debug('No existing contact');
            if (lead.Auto_Convert__c) {
               
    
              MyLead.addCampaign(lead, lead.Campaign__c);
             
              leadConvert.setLeadId(lead.Id);
              leadConvert.setConvertedStatus('Closed - Converted');
              Database.LeadConvertResult lcResult = Database.ConvertLead(leadConvert);
             
              // Populate the newly created records with extra data.
              MyLead.updateOpportunity(lead, lcResult);
              Company__c myCompany = [SELECT Id FROM Company__c WHERE Company_Id__c = :lead.Company_Id__c];
              MyLead.updateContact(lead, lcResult, myCompany);
              // Must update the Company before Account otherwise the Account lookup will fail.
              MyLead.updateCompany(lead, lcResult, myCompany);
              MyLead.updateAccount(lead, lcResult, myCompany);
            }
          }
        }
      }
    }

    // Attaches Lead to Contact and Account if matched by Email, converts Lead and creates opportunity if it doesn't exist.
    public static void attachExistingConvert(Lead lead, Contact contact) {
        MyLead.addCampaign(lead, lead.Campaign__c);
        Database.leadConvert leadConvert = new Database.LeadConvert();
        leadConvert.setLeadId(lead.Id);
        leadConvert.setConvertedStatus('Closed - Converted');
        leadConvert.setContactId(contact.Id);
        leadConvert.setAccountId(contact.AccountId);
        leadConvert.setSendNotificationEmail(True);
        // Do not create an opportunity if any exist.
        Opportunity[] myOpportunity = [SELECT Id FROM Opportunity WHERE AccountId = :contact.AccountId];
        if (myOpportunity.size() > 0) {
          leadConvert.setDoNotCreateOpportunity(True);
        }
        leadConvert.setLeadId(lead.Id);
        Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert);
        // If we created an opportunity, update it with extra stuff.
        if (leadConvert.isDoNotCreateOpportunity() == False) {
          MyLead.updateOpportunity(lead, leadConvertResult);
        }
    }
   
    // Updates the newly created Opportunity with extra data
    public static void updateOpportunity(Lead lead, Database.LeadConvertResult lcResult) {
      Opportunity myOpportunity = [SELECT Id, StageName FROM Opportunity WHERE Id = :lcResult.opportunityId];
      myOpportunity.Name = 'Online Upgrade';
      myOpportunity.TrialStart__c = lead.CreatedDate.date();
      //myOpportunity.Amount = lead.Monthly_Revenue__c;
      myOpportunity.Amount = lead.Amount__c;
      myOpportunity.Type = 'New Business';
      myOpportunity.LeadSource = 'Website';
      myOpportunity.Order__c = lead.Order__c;
      DateTime dt = System.now();
      Date d = dt.date();
      myOpportunity.CloseDate = d;
      myOpportunity.StageName = lead.Agency__c ? 'Won (Agency)' : 'Won (End User)';
      myOpportunity.Probability = 100;

      update myOpportunity;
    }
   
    // Updates the newly created Account with extra data
    public static void updateAccount(Lead lead, Database.LeadConvertResult lcResult, Company__c myCompany) {
      Account myAccount = [SELECT Id FROM Account WHERE Id = :lcResult.accountId];
      myAccount.Type = 'Customer';
      myAccount.ASEO_Company_Id__c = lead.Company_Id__c;
      myAccount.Company__c = myCompany.Id; // Setting the company will cause the company account lookup to fail
      myAccount.Order__c = lead.Order__c;

      update myAccount;
    }
   
    // Updates the newly created Contact with extra data
    public static void updateContact(Lead lead, Database.LeadConvertResult lcResult, Company__c myCompany) {
      Contact myContact = [SELECT Id FROM Contact WHERE Id = :lcResult.contactId];
      myContact.ASEO_Company_Id__c = lead.Company_Id__c;
      myContact.Company__c = myCompany.Id;

      update myContact;
    }

    // Updates the Company with extra data
    public static void updateCompany(Lead lead, Database.LeadConvertResult lcResult, Company__c myCompany) {
      myCompany.Account__c = lcResult.accountId;

      update myCompany;
    }
   
    // Adds a lead to a campaign
    public static void addCampaign(Lead lead, String campaign) {
     
      Campaign[] myCampaigns = [Select Id FROM Campaign WHERE Name = :campaign LIMIT 1];
      if (myCampaigns.size() == 1) {
        CampaignMember campaignMember = new CampaignMember (campaignId=myCampaigns[0].Id, leadId=lead.Id);
        insert campaignMember;
      }
    }
    
    // Create campaign members from leads. Campaign comes from lead.Campaign__c.
    public static void addCampaignMembers(Lead[] leads) {
      for (Lead lead :leads) {
        // Might want to query Campaigns table instead.
        //Set<String> campaigns = new Set<String>();
        //campaigns.add('Freemium');
        Campaign[] myCampaigns = [SELECT Id FROM Campaign WHERE Name = :lead.Campaign__c LIMIT 1];
       
        //if (campaigns.contains(lead.Campaign__c)) {
        if (myCampaigns.size() == 1) {
          MyLead.addCampaign(lead, lead.Campaign__c);
        }
      }
     }
   
}

and trigger on lead 

trigger LeadTrigger on Lead (after insert, after update) {
    Lead[] leads = Trigger.new;
    //MyLead.createOpportunity(leads);
    MyLead.convertLead(leads);
}
MagulanDuraipandianMagulanDuraipandian
Check this

http://www.infallibletechie.com/search/label/Unit%20testing%20in%20Salesforce

If this solves your problem, kindly mark it as the best answer.
Hit Like, if it saved your work :-)

Regards,
Magulan
http://www.infallibletechie.com