Force.com HTTPCallout Test Class

Below is the test class to test an HTTP Callout from apex:

Main Test Class

@isTest
global class HttpCalloutToFeedTest {
	@isTest static void testCallOutToFeed() {
           Test.setMock(HttpCalloutMock.class, new HttpCalloutToFeedMock());
           HttpResponse res = HttpCalloutToFeed.sentMessageToFeed();

           System.assertEquals(201, res.getStatusCode());
	}
}

HTTP Callout Mock

@isTest
global class HttpCalloutToFeedMock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest httpreq) {
      System.assertEquals('http://cloudsole-feed.herokuapp.com/sfdc/public/feed', httpreq.getEndpoint());
      System.assertEquals('POST', httpreq.getMethod());
      String message = '{"name": "Test Class for CloudSole Feed", "message": "This test passed!!!"}';
      
      httpreq.setHeader('Accept', 'application/json');
      httpreq.setHeader('Content-type', 'application/json');
      httpreq.setBody(message);
      httpreq.setMethod('POST');

      HttpResponse res = new HttpResponse();
      res.setStatusCode(201);
      return res;
  }
}

As this is a POST there is no allot of response but the statusCode. 201 means created so the feed was successfully created.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: