0

Trying to wrap my head around this one.

I have a custom object managed by an apex class that has a Process defined along with it. This process has an immediate action that calls an apex class that effectively does equivalent of webhook.

When I write a testClass for it, doing a basic CRUD test, it fails with this message: Methods defined as TestMethod do not support Web service callouts

How does one write a testcase for this?

adding some code:

public class with sharing WebHook {
   public class Ids {
       @InvokableVariable(label='Id')
       public String idfield;
   }

   @InvokableMethod
   public static void postIt(List<Ids> ids) {
      Ids aid = ids[0];
      Map<String, String> m = new Map(...);
      m.put('idfield', aid.idfield);
      System.enqueueJob(new QueueableCall('http://localhost:8000', 'POST', JSON.serialize(m)));
   }

   public class QueueableCall implements System.Queueable, Database.AllowsCallouts {

        private final String url;
        private final String method;
        private final String body;

        public QueueableCall(String url, String method, String body) {
            this.url = url;
            this.method = method;
            this.body = body;
        }

        public void execute(System.QueueableContext ctx) {
            HttpRequest req = new HttpRequest();
            req.setEndpoint(url);
            req.setMethod(method);
            req.setBody(body);
            Http http = new Http();
            HttpResponse res = http.send(req);
        }

    }
}

public class with sharing ClassA {
    public static void addA(String a, String b) {
        ObjA a = new ObjA();
        a.a = a;
        a.b = b;
        insert(a);
    }
}

I'm trying to write a testclass for ClassA and it's erroring with message at top. How do I write tests for WebHook and QueueableCall to complete this test?

  • 1
    You should read this documentation about mock. – Martin Lezer Apr 25 '18 at 15:50
  • I found Trailhead as a better resource for writing test classes to test callouts.trailhead.salesforce.com/en/modules/apex_integration_services/… – Pranay Jaiswal Apr 25 '18 at 15:55
  • Still not getting it. I'm testing Class A and any insert or update to it triggers a Process that as an action calls Class B which does a HTTP Post by implementing System.Queueable, Database.AllowsCallouts. How do I write a test case for Class A? Does it automatically call a test case for Class B as well? – biskit Apr 25 '18 at 16:04
  • added testable code – biskit Apr 25 '18 at 17:12
2

You can break this problem into parts:

  1. Write a testclass for the queueable - you will need to include a class that implements HttpCalloutMock
  2. Write a test class for your invocable method - you will want to verify that it starts the queueable and the queuable is passed the expected arguments - you will be indifferent to the actual callout because you tested this in #1. There are different ways to do this - looking for the presence of AsyncApexJob is one way.
  3. Write a testclass for the condition that launches the Process Builder flow. Here, all you are interested in is that the PB action started the invocable Apex. Asserting this can be tricky as the invocable Apex needs to persist something you can fetch when the test ends (e.g. a custom Log__c or Task record).
| improve this answer | |
0

Between the response above and the tutorial pointed to by @Pranay, I was able to write a test that passed. Did I get coverage? Not checked it yet.

In a nutshell, this is what I did.

  • Created an implementation of HttpCalloutMock
  • Created a test to mock HttpRequest for Webhook.
  • Created a test for the class Class A and called the webhook test here.

Seemed to have passed test.

| improve this answer | |

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