I am pretty new to Apex and trying to learn as much as possible. Any help would be really appreciated. I have two classes.

  1. Invokeable Apex Class: This class is just getting called by a Process (Builder) if lead object meets certain criteria and sending the lead record values like , ID, email, First Name, Last name to another class which is Future Callout class.

  2. @future Apex class is calling a POST rest API (sending the lead data) to an external system and receiving an ID which updates the lead record.

Both the classes are working as expected. I am trying to create Apex Test Class for Both of them but facing issues as i never wrote apex Test class earlier.

Here are the classes

1- Invokable class

public class MakeCallout {

    public static void invokeleadcallout(list<Lead> Leads) {

        //below is the Futurecallout Method
        WS_Lead.Notification(Leads[0].id, Leads[0].Email, Leads[0].Firstname);

2- WS callout class

global class WS_Lead {
  @future (callout=true)
   // Receiving the Lead details from Invokable class ApexCallout.
  WebService static void Notification(id lid, String name)
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();


        //Setting Method Content and Body
        'name='+EncodingUtil.urlEncode(name, 'UTF-8')

        try {
         res = http.send(req);

         Dom.Document docx = new Dom.Document();
         dom.XmlNode xroot = docx.getrootelement();
         String U_Id = xroot.getAttributeValue('U_Id', null);
         system.debug(U_Id );
         Lead le = new Lead(id=lid);
         le.ID__c = U_Id;
         update le;
        catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
  • Can you please mark the answer as accepted so I can award a bounty to sfdcfox
    – Eric
    Mar 6 '17 at 17:21
  • @AdrianLarson - If I add a bounty to reward sfdcfox will the entire amount be given to him or only half since it is not accepted?
    – Eric
    Mar 6 '17 at 17:22

Basically, you need to know about how to test HTTP callouts, and how to test future methods.

Once you've gotten that far, you'll see that the unit test would look like:

@isTest class MakeCalloutTest {
    // Simple echo callout class
    // Returns whatever response we tell it to when asked
    class EchoHttpMock implements HttpCalloutMock {
        HttpResponse res;
        EchoMock(HttpResponse r) {
            res = r;
        // This is the HttpCalloutMock interface method
        public HttpResponse respond(HttpRequest req) {
            return res;

    @isTest static void test() {
        // Avoid using live data
        List<Lead> leads = new List<Lead>{ new Lead(LastName='Test',Company='test') };
        insert leads;
        // We tell it what to simulate
        HttpResponse res = new HttpResponse();
        res.setBody('<?xml version="1.0" encoding="utf-8"?><root U_Id="12345"></root>');
        // This allows the callout to succeed
        Test.setMock(HttpCalloutMock.class, new EchoHttpMock(res));
        // Start the test
        // Enqueue the future call
        // Trigger future method
        // Verify logic
        leads = [select id__c from lead];
        System.assertEquals('12345', leads[0].Id__c);

Note: This doesn't cover the "catch" part of your try-catch block. Once you've gotten as far as covering most of your code, the catch block should be pretty simple (hint: you can construct and throw CalloutException manually from a HttpCalloutMock).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.