20

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?

6

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

| improve this answer | |
  • 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 Apr 3 '14 at 21:05
  • 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 Jul 19 '17 at 18:32
  • ok, I will follow @Jose. Thanks for your quick response...lol – Bforce Aug 10 '17 at 11:06
21

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).

| improve this answer | |
  • 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 Nov 28 '16 at 23:23
  • @Codezilla, another option would be to use a @testSetup method which would setup the Test.setMock – Tim Lewis Apr 14 '17 at 18:40
  • 2
    @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. – Alex Tennant Apr 14 '17 at 22:16
  • Can we use a Test.isRunningTest to call Test.setMock ? – Fernando Gavinho Jan 23 '19 at 16:05
1

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

static Lead lead1;

@TestSetup
static void makeData(){
        Lead lead = new lead(
            FirstName = 'Test First Name',
            LastName = 'Sample Last Name',
            Phone = '555-555-5555',
            MobilePhone='+17072766457',
            Company = 'ananimforceTest'
        );
        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();
}
| improve this answer | |
0

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.

| improve this answer | |

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.