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
Bms270Bms270 

How to cover webservice callout functions in test methods?

I have a class which contains functions that are used to make webservice callouts only. I need to cover these functions in my test methods but "Run Test" fails after running the first function with the following message:

(Older API) Methods defined as TestMethod do not support Web service callouts, test skipped

 

(NEW API - 15) System.TypeException: Testmethods do not support webservice callouts.

 

in this scenario I need to know how I can obtain the required coverage (75%) on my Apex class.  is there anyway that I can ignore callouts but force the test method to continue? in my class more than 75% of the code is the functions that each make a callout to an external webservice. any Idea?
 

Thanks

 

P.S: I have searched the forum and this has been posted several times but no answer yet.

gv007gv007
if (!isTestExecution) 

try puting yours webservice call outs  usinf  istestexcution

 

try

{

 

web service callouts

 yours code

if(!isTestExcution)

}

 

catch {

exceptions 

 

}

check yours response code in the if condition (this code is varies depends on problem).try it.

 

Bms270Bms270

Hi,

 

This could be a nice solution if it was working ;) when I use it I get the following error:

 

Error: Compile Error: Variable does not exist: isTestExecution at line 9 column 17

 

do you have any reference to this "isTestExecution" system variable? I haven't heard about this before.

 

 

 

Thanks

gv007gv007
public Boolean getIsTestExecution() { return isTestExecution; } public void setIsTestExecution(Boolean boolean variablen) { isTestExecution = boolean variable; } declare boolena variable and assign it as false.it works fine.
gv007gv007
that is declare setter and getter methods.
Bms270Bms270
Thanks for the reply. so you mean I should define this boolean var in the class and set it in the test method so functions can read it and not executing the webservices right? I understand but this totally makes a mess in the code. This way I have to write if and else all throughout the code just to pass freaking stupid test? I hate this... but thanks for the solution. seems to be the only way...
gv007gv007

Their is two ways to implment test coverage for webservice callout testing the test excution other one is according to their documentation.see the test coverage documentation they provided how to implement webservice callouts and test coverage.thanks

SForceBeWithYouSForceBeWithYou

Please, use this in your test method and all will be resolved....

 

StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
mock.setStaticResource('NameOfStaticResourceContainingResponseBodyString');
mock.setStatusCode(200); // Or other appropriate HTTP status code
mock.setHeader('Content-Type', 'application/json'); // Or other appropriate MIME type like application/xml
Test.setMock(HttpCalloutMock.class, mock);

 Here is the link to the implementation this uses:

http://www.salesforce.com/us/developer/docs/apexcode/index.htm

 

Here is the section that covers this in general:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_testing.htm

 

May The SForce Be With You!

 

Nathan Pepper

youtube.com/MayTheSForceBWithYou

@SForceBeWithYou