1

Error MessageWe have a custom object called 'support' - we have process builder configured for this object. in one of the process builder action it will invoke Apex (Invocable Method). This will trigger a callout to External System. In Apex Test Class we have mocked the response for HTTP callout by using Test.setMock & HttpCalloutMock interface. code coverage for that apex class is 96%.

We have different apex test classes where we are need test records for Support Object - we are creating test data using testDataFactory Class. While running the test we are seeing below error - "Methods defined as TestMethod do not support Web service callouts".

Please provide your suggestion to resolve this issue.

Sample Code:

public class AnimalsCallouts {

  @InvocableMethod(label='Call External System')
  public static void callEx(List<ID> ids) {
      makeGetCallout();
  }


    @Future(callout=true)
    public static void makeGetCallout() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
            // Deserializes the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'animals' key as a list
            List<Object> animals = (List<Object>) results.get('animals');
            System.debug('Received the following animals:');
            for (Object animal: animals) {
                System.debug(animal);
            }
        }
        return;
    }
}

@isTest
global class AnimalsHttpCalloutMock implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}');
        response.setStatusCode(200);
        return response; 
    }
}


@isTest
private class AnimalsCalloutsTest {
    @isTest static void testPostCallout() {
        // Set mock callout class 
        Test.setMock(HttpCalloutMock.class, new AnimalsHttpCalloutMock()); 
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock. 
        AnimalsCallouts.callEx(null);
    }
}

@isTest
public class TestFactory {
  public static Support__c getSupport(String name, Boolean doInsert){
    Support__c a = new Support__c(name = name);
    if(doInsert){
      insert a;
    }
    return a;
  }
}

@IsTest
private class NewTestClass {
    @TestSetup
    static void dataCreation(){
      Support__C a = TestFactory.getSupport('Muddy Water', true);
    }
    @isTest static void testMethod111() {
        // do Nothing
    }
}

marked as duplicate by Eric apex Oct 22 '18 at 7:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • cannot say anything without going through your code, can you please post in detail? – rahul gawale Oct 21 '18 at 17:07
  • you don't want to run process builder for some test methods. right? – Pragati Jain Oct 21 '18 at 17:20
  • @PragatiJain - yes i want to skip process Builder - is there any way to achieve this ? – Red Rick Oct 21 '18 at 17:42
  • @rahulgawale - I have updated my question with the sample code. – Red Rick Oct 21 '18 at 18:21
  • @RedRick did the answer by Pragati work for you ? – rahul gawale Oct 22 '18 at 6:04
3

You can disable Process Builders in apex by adding a switch. For this you have to create a custom setting (protected) which have a boolean field (with default value true). Then update your Process Builders so that the very first entry criterion is the custom setting being true, and if it is not, stop. Then from the Apex Test Method, you will be able to set the Custom Setting field to false whenever you need to stop the processes from executing.

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