You need to sign in to do that
Don't have an account?
I Need ANSWER! 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.
Hi,
i dnt find any problem , i just wrote test method for webservice callouts for managed package creation..It never gave any problem..Is it something to do with future calls?? Are u using future callouts??
Hi,
The issue is when a test method hits the webservice callout (not @future) it stops the test. here's a sample code:
WebService static void synchWith(String[] product,String command,String acid){
....
if(command.equalsIgnoreCase('delete')){
webservicesCommerceItems.ArrayOfString iids = new webservicesCommerceItems.ArrayOfString();
iids.string_x = new String[1];
iids.string_x[0]=product[0];
stub.delete1(iids); //------------------->****This is a call to a webservice which throws the exception
}else if(command.equalsIgnoreCase('insert')){
webservicesCommerceItems.Items items = new webservicesCommerceItems.Items();
items.vendorid=store_account[0].StoreVendorId__c;
items.code=product[1];
........
When the test method gets to that line it stops and does not go to the next call.
static testMethod void classTest() {
synchWith(items,'delete','xx');
synchWith(items,'insert','xx'); --> this does not execute due to an exception on the previous function call.
}
This way I cannot cover the big portion of the code. I have also found some workaround here:
http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods#Test_Methods_and_Apex_Callouts
but this is not doable in some cases when there is a dependency between service calls. any idea?
I am just returning fake http responses - if I am in a testmethod.
However, I have put a custom field in each object of interest (in_test_method__c) to do this.
I don't see a way for in Apex to find out if I am inside a testMethod.
I did the same thing as GoForceGo, with one slight difference. Instead of adding a custom field, I added a public static Boolean field to my class under test called "underTest". In my test class, I set "underTest" to true before constructing my class under test. In the class under test, I have if/else conditionals to bypass the Http callout and generate bogus Http reponses when "underTest" is true.
TLF,
I realized this and and did the same.
I created another class to handle this - by default inTestmethod is false, and I set it as true.
public class setInTestMethod {
private static boolean InTestMethod = false;
public static void setInTestMethod(boolean InTest) {
InTestMethod = inTest;
}
public static boolean getInTestMethod() {
return InTestMethod;
}
}
if (!System.Test.isRunningTest()) {
WebServiceCallout.invoke(...)
} else {
// Simulate callout here...
}