Ajax (1) Apex Class (12) Apex Trigger (2) Community (2) Home Page (1) HTML (4) Integration (3) JS (7) KB (1) Label (1) Licenses (1) Listing (1) Log (1) OOPs (5) Sharing (1) Static Resource (1) Test Class (3) URI (1) Visualforce (10)

Thursday 19 June 2014

Mock Call Out Class

Open topic with navigation

Testing HTTP Callouts by Implementing the HttpCalloutMock Interface

Provide an implementation for the HttpCalloutMock interface to specify the response sent in the respond method, which the Apex runtime calls to send a response for a callout.

    global class YourHttpCalloutMockImpl implements HttpCalloutMock {      global HTTPResponse respond(HTTPRequest req) {          // Create a fake response.
        // Set response values, and
        // return response. } }
Note
  • The class that implements the HttpCalloutMock interface can be either global or public.
  • You can annotate this class with @isTest since it will be used only in test context. In this way, you can exclude it from your organization’s code size limit of 3 MB.

Now that you have specified the values of the fake response, instruct the Apex runtime to send this fake response by calling Test.setMock in your test method. For the first argument, pass HttpCalloutMock.class, and for the second argument, pass a new instance of your interface implementation of HttpCalloutMock, as follows:

    Test.setMock(HttpCalloutMock.class, new YourHttpCalloutMockImpl());

After this point, if an HTTP callout is invoked in test context, the callout is not made and you receive the mock response you specified in the respond method implementation.

Note
If the code that performs the callout is in a managed package, you must call Test.setMock from a test method in the same package with the same namespace to mock the callout.

This is a full example that shows how to test an HTTP callout. The interface implementation (MockHttpResponseGenerator) is listed first. It is followed by a class containing the test method and another containing the method that the test calls. The testCallout test method sets the mock callout mode by calling Test.setMock before calling getInfoFromExternalService. It then verifies that the response returned is what the implemented respond method sent. Save each class separately and run the test in CalloutClassTest.

    @isTest                          
global class MockHttpResponseGenerator implements HttpCalloutMock { // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) { // Optionally, only send a mock response for a specific endpoint
        // and method. System.assertEquals('http://api.salesforce.com/foo/bar', req.getEndpoint()); System.assertEquals('GET', req.getMethod()); // Create a fake response HttpResponse res = new HttpResponse(); res.setHeader('Content-Type', 'application/json'); res.setBody('{"foo":"bar"}'); res.setStatusCode(200); return res; } }
    public class CalloutClass {      public static HttpResponse getInfoFromExternalService() {          HttpRequest req = new HttpRequest();          req.setEndpoint('http://api.salesforce.com/foo/bar');          req.setMethod('GET');          Http h = new Http();          HttpResponse res = h.send(req);          return res;      }  }
    @isTest                          
private class CalloutClassTest { @isTest static void testCallout() { // Set mock callout class Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock. HttpResponse res = CalloutClass.getInfoFromExternalService(); // Verify response received contains fake values
        String contentType = res.getHeader('Content-Type'); System.assert(contentType == 'application/json'); String actualValue = res.getBody(); String expectedValue = '{"foo":"bar"}'; System.assertEquals(actualValue, expectedValue); System.assertEquals(200, res.getStatusCode()); } }
© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.

 

No comments:

Post a Comment