HTTPCallout Test Class

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

Main Test Class

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

global class HttpCalloutToFeedMock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest httpreq) {
      System.assertEquals('', 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');

      HttpResponse res = new HttpResponse();
      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.

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s