function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Antoine LeleuAntoine Leleu 

Test a customer controller calling an HttpCallout Class ?

Hi all,

I developed a Http Callout Class. I can cover this code at 100% with a test method using MockHttpResponseGenerator.
Then i developed a custom controller calling this Http Callout Class and i'm trying to develop the test metohd for this controller.

If i run my test, i obtain this error "Methods defined as TestMethod do not support Web service callouts, test skipped". 
So, i read in the forum that i had to add a condition on my http call out call like this "if (!System.Test.isRunningTest())" to execute my test.

But in this case, my Call Out class is not covered at 100% cause the lines between the condtion can be executed.

Has someone already encountered this problem ? Is there any solution or workaround for this ?

Thanks,

Antoine

 
James LoghryJames Loghry
No!!!  Bad, Antoine, bad! There is little to no reason for using Test.isRunningTest in your test code.

If you have a "MockHttpResponse" class, then you're most of the way there.

In your test, you just have to pass that in to your Test.setMock() call before running Test.startTest().

See the last example here: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm