Salesforce Tip: HTTP Callouts test with HttpCalloutMock

In one of the previous post, I shared a workaround to simulate the HTTP request response in Test class. The method is not elegant but at least it works for me 😀

 

In Salesforce Winter’13 release, Salesforce has introduced a new Apex class, HttpCalloutMock where it allows you to create a proper test method for HTTP callouts. Below are the high level steps that you need to do to simulate the HTTP callout in your test class:

  1. create a class that implements HttpCalloutMock with a sample response
  2. initialize the HttpCalloutMock class in your test class by using Test.setMock() method

 

For more details, please see the official documentation.