17

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

  • 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
18

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

  • 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

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.