Hello friends, In this post, I will talk about a exception (Methods defined as TestMethod do not support Web service callouts) that happen when you write a test class for @future annotation callouts. After going through this post you will aware, why this exception happend and how to solve this in test class. So let's get started

Methods defined as TestMethod do not support Web service callouts

Sample class with @future annotation with callout=true

 public class SampleClass {        
@future(callout=true)
public static void sampleMethod(String param){
//......Code goes here........
}
}
Following is the test class for above sample class.
 @isTest
public class SampleClassTest {
static testMethod void testsample(){
SampleClass.sampleMethod('Test');
}
}

Following error will happen when we normally call the future method in test class to get code coverage.

Solution: To resolve the above issue we have to made some changes in test class that look like following code sample.

 @isTest
public class SampleClassTest {
public class ExampleCalloutMock implements HttpCalloutMock{
public HttpResponse respond(HTTPRequest req){
HttpResponse res = new HttpResponse();
res.setStatus('OK');
res.setStatusCode(200);
res.setBody('Salesforcescool');
return res;
}
}

static testmethod void testsample(){
Test.startTest();
Test.setMock(HttpCalloutMock.class, new ExampleCalloutMock());
SampleClass.sampleMethod('test');
Test.stopTest();
}
}

Invoke your webservice/callout methods under Test.startTest(); & Test.stopTest();, in between use Test.setMock(HttpCalloutMock.class, new ExampleCalloutMock()). 

Test.stopTest() does not return until your future method has completed.

Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.