33

I have been struggling with this error for a few days now. I get this error every time I try to run a test for a method that has an httpCallout. I followed the architecture from http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_testing_httpcalloutmock.htm, but to no avail. I thought it could be something I was messing up in my code, so I copied all three of the classes from the documentation, and I am still getting the error when I try to run the test. All of the classes are on Salesforce API 29. Has anyone run into this before?

0

4 Answers 4

4

you may want to include test.isrunningtest in your actual callout class to check you are not going to run actual callout and only test mock. take a look on this thread

Testing HttpCallout with HttpCalloutMock and UnitTest Created Data

3
  • salesforce.stackexchange.com/a/5070/7709 this answer did it! Thanks so much! You are a god among men! I was banging my head against the desk with this error for a while now.
    – Mr. Ant
    Commented Apr 3, 2014 at 21:05
  • 2
    Although this fixes the problem, you shouldn't do this in must cases. You'll want to actually test your code with a mock response and should follow alex answer.
    – Jose
    Commented Jul 19, 2017 at 18:32
  • ok, I will follow @Jose. Thanks for your quick response...lol
    – Bforce
    Commented Aug 10, 2017 at 11:06
40

You need to call Test.setMock(...) in your test class once you've implemented the required interfaces to prevent this particular error message.

You shouldn't need to use Test.isRunningTest() to test your call outs (and doing so give you untestable code).

4
  • 3
    If I used Test.setMock(), wouldn't I have to put that in every single test class that might end up making a callout due to triggers and flows? Wouldn't one concise Test.isRunningTest() check that prevents the actual callout or creates a mock at that point be more reasonable? It seems unreasonable to have to go back and rewrite 20 test classes because I added a trigger.
    – Codezilla
    Commented Nov 28, 2016 at 23:23
  • @Codezilla, another option would be to use a @testSetup method which would setup the Test.setMock
    – Tim Lewis
    Commented Apr 14, 2017 at 18:40
  • 3
    @TimLewis That will not work. Only the state of the database is retained from @testSetup. Test.setMock(), Test.setCurrentPage() etc do not work in the way you'd like. Commented Apr 14, 2017 at 22:16
  • Can we use a Test.isRunningTest to call Test.setMock ? Commented Jan 23, 2019 at 16:05
6

Methods defined as TestMethod do not support Web service callouts Stack Trace: null

This error occurs because web service callouts are not allowed in Test class.

To bypass callouts add HttpcalloutMock class.

Here is a sample test class with the mock class to bypass web services Callout.

********************** Test Class ***************************

@isTest 
private class Test_class {

    private class RestMock implements HttpCalloutMock {

        public HTTPResponse respond(HTTPRequest req) {
            String fullJson = 'your Json Response';

            HTTPResponse res = new HTTPResponse();
            res.setHeader('Content-Type', 'text/json');
            res.setBody(fullJson);
            res.setStatusCode(200);
            return res;
        }
    }
    static testMethod void service_call() {

        Test.setMock(HttpCalloutMock.class, new RestMock());
        Test.startTest();

        //your web service call code

        Test.StopTest();


    }
}

The mock class will take care of webservice callouts.

3

You can't create DML statements before calling your test API callouts. You should put all your DMLs in a separate function with a @TestSetup

@TestSetup
static void makeData(){
    Lead lead = new lead(
        FirstName = 'Test First Name',
        LastName = 'Sample Last Name',
        Phone = '555-555-5555',
        MobilePhone='+15555555555',
        Company = 'Some Test Company'
    );
    insert lead;
    lead1 = [SELECT Id, FirstName, LastName, Phone, MobilePhone FROM Lead LIMIT 1];
}

@istest
static void Controller_CodeCoverage_Test() {
    Test.setMock(HttpCalloutMock.class, new RequestCalloutMockClass());
    Test.startTest();
    // your web service call code
    Test.stopTest();
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .