I have a batch class which contains HTTP callout, I have seen the blogs for it httpcalloutmock, But I am not understanding how to do it in batch class. Any help is very much appreciated, thank you.


You just need to execute the batchable:

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

// Any data setup goes here

Database.executeBatch(new YourBatchable());

and the HttpCalloutMock will get called from your asynchronously executed batchable because the Test.stopTest that ensures:

All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously.

  • @isTest public with sharing class MetadataBatchTest{ static testmethod void metadataBatchTestMethod(){ Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); Test.startTest(); MetadataBatch batchObj = new MetadataBatch('SELECT Id FROM Custom_String__c'); Id processId = Database.executeBatch(batchObj,200); Test.stopTest(); } } Apr 25 '16 at 8:27
  • This my test class and it still gives me the same error " Methods defined as TestMethod do not support Web service callouts" Apr 25 '16 at 8:28
  • @MarufBagwan I don't know the cause of the problem. I suggest you ask a new question including the source code of both the test and the batchable and any line number information that is part of the error.
    – Keith C
    Apr 25 '16 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.