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
Damon HedgspethDamon Hedgspeth 

Help with testing Lead Convert Trigger Code

I have a trigger on lead convert that creates a record in a managed package app.  When I run my test code, i get the following error: "Methods defined as TestMethod do not support Web service callouts". I have tried the httpmock code and istestrunning.  What am I missing?

Here is my trigger
trigger LeadConvert on Lead (after Insert,after update) {
   String recordTypeName = 'ID Lead Record Type'; // <-- Change this to your record type name
  
  Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Lead.getRecordTypeInfosByName();
  Schema.RecordTypeInfo rtInfo =  rtMapByName.get(recordTypeName);
  id recordTypeId = rtInfo.getRecordTypeId();
 if(!test.isrunningtest()) {
 For(Lead o : Trigger.New){
 If(o.RecordTypeId == recordTypeId){
 
  // no bulk processing; will only run from the UI
  if (Trigger.new.size() == 1) {

    if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {

      // if a new account was created
      if (Trigger.new[0].ConvertedAccountId != null) {

        // update the converted account with some text from the lead
        Account a = [Select a.Id, a.Description From Account a Where a.Id = :Trigger.new[0].ConvertedAccountId];
        a.Description = Trigger.new[0].Name;
        update a;

      }          

      // if a new contact was created
      if (Trigger.new[0].ConvertedContactId != null) {

        // update the converted contact with some text from the lead
        Contact c = [Select c.Id, c.Description, c.Name From Contact c Where c.Id = :Trigger.new[0].ConvertedContactId];
        c.Description = Trigger.new[0].Name;
        update c;
         
If(o.Create_Apttus_Proposal__c == True) {
        // insert a custom object associated with the contact
       
        Apttus_Proposal__Proposal__c obj = new Apttus_Proposal__Proposal__c();
        obj.Apttus_Proposal__Proposal_Name__c = c.Name;
        obj.Apttus_Proposal__Description__c = c.Description;
        obj.Apttus_Proposal__Account__c = Trigger.new[0].ConvertedAccountId;
        obj.Due_to_Funder__c = Date.today().addDays(+60);
        obj.Program__c = Trigger.new[0].Program__c;
        obj.Proposal_Reviewer_list__c = Trigger.new[0].Proposal_Reviewer_list__c;
        obj.Total_Proposal_Value__c = 1;
        obj.Apttus_Proposal__ExpectedStartDate__c = Date.today().addDays(+60);
        obj.Apttus_Proposal__ExpectedEndDate__c = Date.today().addDays(+365);
        obj.Account_Contact__c = Trigger.new[0].ConvertedContactId;
        obj.Lead_Id__c = c.id;
        obj.Proposal_Status__c = 'Awaiting RFP';
        insert obj;
}}{
      }

     

   } }

  }     

}
}
}
Here is my test code:
 
@isTest
public class LeadConvertTest {
   
    static testMethod void insertNewLead() {

        Lead newLead = new Lead();
        newLead.LastName = 'Lee';
        newLead.FirstName = 'James';
       newLead.Company = 'TestTrigger';
       newLead.Program__c = 'Assessment & Standards Development Services';
       newLead.Proposal_Reviewer_list__c = 'Aida Walqui';
       newLead.Create_Apttus_Proposal__c = True;

        insert newLead;
       
     test.startTest();
      Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
      

        Database.LeadConvert lc = new Database.LeadConvert();
       lc.setLeadId(newLead.id);

        
        

       LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
//lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setConvertedStatus('Qualified');


       Database.LeadConvertResult lcr = Database.convertLead(lc);
        
        System.assert(lcr.isSuccess());

      test.stopTest();
    }

}


 
Best Answer chosen by Damon Hedgspeth
neeraj singh 20neeraj singh 20
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator())- This is a mock Service for mocking ant HTTP callout
For Mocking Webservice callouts -Test.setMock(WebServiceMock.class, new YourWebServiceMockImpl());

YourWebServiceMockImpl - This is work mock class for webservices call.

On how to create this please refer to the sample developer doc below:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm

All Answers

neeraj singh 20neeraj singh 20
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator())- This is a mock Service for mocking ant HTTP callout
For Mocking Webservice callouts -Test.setMock(WebServiceMock.class, new YourWebServiceMockImpl());

YourWebServiceMockImpl - This is work mock class for webservices call.

On how to create this please refer to the sample developer doc below:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
This was selected as the best answer
Lars NielsenLars Nielsen
I know how it goes when you are in hury - the last thing you want to do is go through a whole tutorial. You might just quickly look at this one Module on Salesforce Trailhead because they make extensive use the mock callouts.
https://developer.salesforce.com/trailhead/en/module/apex_integration_services
Damon HedgspethDamon Hedgspeth
No help there as it doesn't consider my exact code.  This seems to be a simple issue with a complicated solution.  Any ideas on my exact code?
neeraj singh 20neeraj singh 20
Plz check whether ther is any trigger on lead/Apttus_Proposal__Proposal__c  which does a webservice call. A important info that i would like to share is thet lead convert is a future method and would commence after it encounters Test.stopTest() in test class so you may want to create and convert the lead in a data Set up method and insertNewLead() have the Webservice mockClass() along with Asserts.