3

Good day everyone,

I am trying to create a restful web service callout everytime a contact in created,updated or deleted. I used this link as my reference http://blog.jeffdouglas.com/2009/03/16/restful-web-service-callout-using-post/ and it working fine. The first problem I have is that how to create a test class.I've encounter this error.

System.TypeException: Methods defined as TestMethod do not support Web service callouts, test skipped

So, I searched in the internet and found a solution which is to create a class that implement the HttpCalloutMock interface. Below is code I found.

@isTest
public class AS_webServiceCallout implements HttpCalloutMock {
        protected Integer code;
        protected String status;
        protected String bodyAsString;
        protected Blob bodyAsBlob;
        protected Map<String, String> responseHeaders;

        public AS_webServiceCallout(Integer code, String status, String body,
                                         Map<String, String> responseHeaders) {
            this.code = code;
            this.status = status;
            this.bodyAsString = body;
            this.bodyAsBlob = null;
            this.responseHeaders = responseHeaders;
        }

        public AS_webServiceCallout(Integer code, String status, Blob body,
                                         Map<String, String> responseHeaders) {
            this.code = code;
            this.status = status;
            this.bodyAsBlob = body;
            this.bodyAsString = null;
            this.responseHeaders = responseHeaders;
        }

        public HTTPResponse respond(HTTPRequest req) {
            HttpResponse resp = new HttpResponse();
            resp.setStatusCode(code);
            resp.setStatus(status);
            if (bodyAsBlob != null) {
                resp.setBodyAsBlob(bodyAsBlob);
            } else {
                resp.setBody(bodyAsString);
            }

            if (responseHeaders != null) {
                 for (String key : responseHeaders.keySet()) {
                resp.setHeader(key, responseHeaders.get(key));
                 }
            }
            return resp;
        }
}

And inside my test classes where I have to create,update or delete a contact I will a code which is something like this.

@isTest

private class AS_testApplicant{

    static testMethod void testApplicant() {
        // this is the code I include for webservice callout
        AS_webServiceCallout fakeResponse = new AS_webServiceCallout(200,
                                                 'Complete',
                                                 '[{"Name": "sForceTest1"}]',
                                                 null);
        Test.setMock(HttpCalloutMock.class, fakeResponse);

        Contact cntct = new Contact();
        cntct.LastName = 'Sample jet';
        cntct.Email = '[email protected]';
        insert cntct;
    }
}

Everything is working fine at the moment. But the thing in my mind is that, am I in the right path ? What if someone will create a class or test class that will include the contact object do they need this code also ? Is there any way to make only one test class for this so that the other test classes will not do it again ? Please help.

1
  • 2
    Yes you are on the right track.
    – Eric
    Commented Apr 27, 2016 at 4:13

1 Answer 1

3

You are in the right direction. Stick with the mock code you have, that is the proper way of unit testing.

What you could do is have a dedicated test class like you started. Then, in all other tests, refer to a common "testUtility" that will configure data upfront before each test method: testUtility.CreateEnvironment();. Your other test methods would call this common class and a method in it to "prep" the data and settings. The setting that I'd suggest is that before performing the callout you check a public static flag that you can manage like if(Util.PerformCallouts) // continue

In the CreateEnvironment method you would always set Util.PerformCallouts = false.

Go more granular if needed too, PerformContactCallout etc.

Here is an in-depth Dreamforce session on handling multiple callouts which you may run into: http://blog.crmscience.com/2015/09/enhanced-web-service-testing-better.html

And the original blog article that points out ways to handle multiple callouts with multiple mocks. http://blog.crmscience.com/2014/12/test-coverage-pattern-for-multi-callout.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .