[SalesForce] Unit Test Error: Methods defined as TestMethod do not support Web service callouts

Firstly, I know what this message is trying to tell me:

Methods defined as TestMethod do not support Web service callouts

And I believe I have set up all the HttpCalloutMock's correctly, because the other 40 unit tests which failed are now passing.

I have a class/method MockFactory.create() which creates StaticResourceCalloutMock for the 4 end points I call out to.

@isTest
public class MockFactory {

    public static HttpCalloutMock create() {

        StaticResourceCalloutMock contactMock = getStaticMock(200, 'ContactsMock');
        StaticResourceCalloutMock invoiceMock = getStaticMock(200, 'InvoicesMock');
        StaticResourceCalloutMock creditNoteMock = getStaticMock(200, 'CreditNotesMock');
        StaticResourceCalloutMock paymentMock = getStaticMock(200, 'PaymentsMock');

        Map<String, HttpCalloutMock> mocksMap = new Map<String, HttpCalloutMock>();

        mocksMap.put('https://api.example.com/api/2.0/Contacts', contactMock);
        mocksMap.put('https://api.example.com/api/2.0/Invoices', invoiceMock);
        mocksMap.put('https://api.example.com/api/2.0/CreditNotes', creditNoteMock);
        mocksMap.put('https://api.example.com/api/2.0/Payments', paymentMock);

        HttpCalloutMock mocks = new MultiRequestMock(mocksMap);

        return mocks;
    }

    private static StaticResourceCalloutMock getStaticMock(Integer statusCode, String staticResourceName) {

        StaticResourceCalloutMock mock = new StaticResourceCalloutMock();

        mock.setStaticResource(staticResourceName);
        mock.setStatusCode(statusCode);
        mock.setHeader('Content-Type', 'application/json');

        return mock;
    } 
}

And the MultiRequestMock class should throw an error if it trys to do a callout to an endpoint there isn't a mock setup for:

@isTest 
public class MultiRequestMock implements HttpCalloutMock {

    Map<String, HttpCalloutMock> requests;

    public MultiRequestMock(Map<String, HttpCalloutMock> requests) {
        this.requests = requests;
    }

    public HTTPResponse respond(HTTPRequest req) {

        HttpCalloutMock mock = requests.get(req.getEndpoint());

        if (mock != null) {
            return mock.respond(req);
        } 

        throw new MultiRequestMockException('Mock for end point not found: ' + req.getEndpoint());
    }

    public void addRequestMock(String url, HttpCalloutMock mock) {
        requests.put(url, mock);
    }

    public class MultiRequestMockException extends Exception {}
}

But one set of tests that relate to testing a Schedulable class are all still failing.

What have I tried?

I've tried bypassing the Schedulable class and calling the child directly, and it still does not work.

Before

@isTest
public static void test_inactivePerson() 
{  
    Test.setMock(HttpCalloutMock.class, MockFactory.create());

    // arrange
    // create some objects...

    // act
    Test.StartTest();
    System.schedule('Test Schedulable', '0 0 23 * * ?', new StatementSchedulable()); 
    Test.stopTest(); 

    // assert
}

After

@isTest
public static void test_inactivePerson() 
{  
    Test.setMock(HttpCalloutMock.class, MockFactory.create());

    // arrange
    // create some objects...

    // act
    Test.StartTest();
    Statements.generate();
    Test.stopTest(); 

    // assert
}

QUESTION

What else could be caused the error message to be returned?

Best Answer

This issue was caused by test data created within the @testSetup method.

An object was being created with a status which was triggering Process Builder to make the call out.

All of this is running before I was calling on the first line of the test method.

Test.setMock(HttpCalloutMock.class, MockFactory.create());

Respect to @NavalSharma

Related Topic