You need to sign in to do that
Don't have an account?
EXRADM
testMethod coverage with callouts
I'm trying to finish up my testMethods on a Controller Extension I wrote for a Visualforce page. The problem I'm running into is that the controller is involved in a lot of callouts. There are a couple of Geocoding callouts to Google as well as 4 different callouts to our point of sale system. Obviously testMethods don't allow callouts, but how can I get my coverage to 75% (currently at 48%) when so much of the code is tied up in callouts? Suggestions?
Unfortunatly, you cannot create a HttpResponse Object, but how i got around it is by
declaring a static vars:
boolean IS_TESTMETHOD - set this to true when running a testmethed,
string TESTRESPONSE - Expected response, you may want to have other responses if you have a lot of code handling exceptions.
Example code:
public virtual Geography.GeoCodeResults geocode(Geography.Address aAddress) { string stat; if (Geography.IS_TESTMETHOD) { stat='200'; } else { callService_GeoCode(aAddress); stat = String.valueOf(response.getStatusCode()); } if (stat == '200') return new Geography.GeoCodeResults(true,stat,'',parse()); else return new Geography.GeoCodeResults(false,stat,response.getStatus(),null); } public virtual Geography.GeoAddresses parse() { if (Geography.IS_TESTMETHOD) return parseBody(TEST_RESPONSE); else if (response == null) return null; else return parseBody(response.getBody()); }
All Answers
interesting qn ... i only thought about it when i read the article on testing ... i would also like to know the answer .. if you get the solution to this problem of yours, please post both the apex class as well as test class (/method).
Thanks
Unfortunatly, you cannot create a HttpResponse Object, but how i got around it is by
declaring a static vars:
boolean IS_TESTMETHOD - set this to true when running a testmethed,
string TESTRESPONSE - Expected response, you may want to have other responses if you have a lot of code handling exceptions.
Example code:
public virtual Geography.GeoCodeResults geocode(Geography.Address aAddress) { string stat; if (Geography.IS_TESTMETHOD) { stat='200'; } else { callService_GeoCode(aAddress); stat = String.valueOf(response.getStatusCode()); } if (stat == '200') return new Geography.GeoCodeResults(true,stat,'',parse()); else return new Geography.GeoCodeResults(false,stat,response.getStatus(),null); } public virtual Geography.GeoAddresses parse() { if (Geography.IS_TESTMETHOD) return parseBody(TEST_RESPONSE); else if (response == null) return null; else return parseBody(response.getBody()); }