0

I'm attempting to include a mock web service callout in a test for a trigger, though the trigger and test don't explicitly execute a callout. I've found this documentation on creating the mock, but I'm pretty new to Apex and am unsure how to configure the two classes: WebServiceMockImpl and WebSvcCallout.

Any guidance would be appreciated. Below is the trigger, an associated class and the current test class.

Trigger:

trigger updateContactAfterConverted on Lead (after update) {

    for(Lead lead:System.Trigger.new) {

        // was the lead converted?

        if (Lead.IsConverted) {

            // query new contact

            Contact newContact = [SELECT Id FROM Contact WHERE Contact.Id = :lead.ConvertedContactId];

            // run @future class to update contact after conversion completed

            updateContactAfterConvertedFuture.myMethod(newContact.id);

        }

    }

}

Associated class:

public class updateContactAfterConvertedFuture {

  @future 
  public static void myMethod(String newContact) {

    // Find new contact

    Contact updateContact = [SELECT Id FROM Contact WHERE Contact.Id = :newContact LIMIT 1];

    // Set field to true                                    

    updateContact.Conversion_Completed__c = TRUE;

    // Update contact                

    update updateContact;     

   }

}

Test class:

@isTest
private class testUpdateContactAfterConvertedOrig {

    static testmethod void myUnitTest() {

            Test.startTest();

    // This causes a fake response to be generated
    Test.setMock(WebServiceMock.class, new WebServiceMockImpl());

    // Call the method that invokes a callout
    String output = WebSvcCallout.callEchoString('Hello World!');

    // Verify that a fake result is returned
    System.assertEquals('Mock response', output);                 

            // Create new test lead
            Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons', LeadSource = 'Advertising', Lead_Source_Detail__c = 'PPC');
            insert myLead;

            // Convert test lead
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(myLead.id);

            // Check conversion
            LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);
            lc.setDoNotCreateOpportunity(True);

            // Declare successful
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            Test.stopTest();
            System.assert(lcr.isSuccess());

    }
}
  • So what is not working. – Tushar Sharma Feb 20 '16 at 8:11
  • You have configured WebServiceMockImpl correctly by registering the mock you want to use. The WebSvcCallout code is just an example of code that calls the web service. Can you confirm that you have added code that calls your web service somewhere? A common problem with this sort of test is "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out" - update your question if you are getting that. – Keith C Feb 20 '16 at 8:56
  • Sorry -- what's not working is that when I try to deploy my trigger to production (without the callout mock), I get this error: "Methods defined as TestMethod do not support Web service callouts" -- which I believe means that existing code already deployed in production is making a callout during the lead conversion process. So I wanted to implement the mock to satisfy the requirements of the test. I don't need to make a callout in my trigger itself. – Matt Feb 20 '16 at 20:42
  • Related question with the problem: Methods defined as TestMethod do not support Web service callouts – Daniel Ballinger Feb 21 '16 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.