Friday 22 March 2013



Apex Code has built in functionality to call external Web services, such as Amazon Web Services, Facebook, Google, or any publicly available web service. You will also need to have the proper test method code coverage for the related Apex code that makes these callouts.

But since the Force.com platform has no control over the external Web service and the impact of making the web service call, test methods can not invoke a 3rd party web service. This section provides a viable workaround to ensure proper code coverage.


With Winter 13, you can now use mock tests with a response that is either hard-coded or reading static resource, like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class demoTestWSCallouts {
    private String URL = 'http://www.eltoro.it/ServerTime';
    public HttpResponse invokeWS() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
      
        Http h = new Http();
        HttpResponse resp = h.send(req);
      
        System.debug(resp.getBody());
        return resp;
    }
}


Hard-coded response


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@isTest
public class demoTestWSCallouts_ImplementsMock implements HttpCalloutMock {
    private HttpResponse resp;
     
    public demoTestWSCallouts_ImplementsMock(String testBody) {
        resp = new HttpResponse();
        resp.setBody(testBody);
        resp.setStatusCode(200);
    }
     
    public HTTPResponse respond(HTTPRequest req) {
        return resp;
    }
    public testmethod static void testWS() {
        String testBody = 'This is a test :-)';
         
        HttpCalloutMock mock = new demoTestWSCallouts_ImplementsMock(testBody);
        Test.setMock(HttpCalloutMock.class, mock);
        demoTestWSCallouts callout = new demoTestWSCallouts();
        HttpResponse resp = callout.invokeWS();
         
        System.assertEquals(200, resp.getStatusCode());       
        System.assertEquals(resp.getBody(), testBody );
    }
}


Static Resourcse


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@isTest
public class demoTestWSCallouts_UsingStaticResources {
    public testmethod static void testWS() {
        String testBody = 'This is a test :-)';
        StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
        mock.setStaticResource('Test_WS');
        mock.setStatusCode(200);
        Test.setMock(HttpCalloutMock.class, mock);
        demoTestWSCallouts callout = new demoTestWSCallouts();
        HttpResponse resp = callout.invokeWS();
         
        System.assertEquals(200, resp.getStatusCode());       
        System.assertEquals(resp.getBody(), testBody);
    }
}
Categories:

1 comment:

  1. Very good tutorial, but I'd like to make one constructive suggestion for enhancement:

    Please provide a sample file for "Test_WS" so we can see what that should look like and make your last test pass in our environments.

    :-)

    ReplyDelete

    Links