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
EXRADMEXRADM 

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?
Best Answer chosen by Admin (Salesforce Developers) 
TomSnyderTomSnyder

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

rawiswarrawiswar

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

TomSnyderTomSnyder

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()); }

 

 

This was selected as the best answer
EXRADMEXRADM
Thank you!  That's exactly what I was looking for.  Kinda feel dumb now that I know the answer...:smileytongue:  Even though it creates an if branch that would never receive coverage, it would take a heck of a lot of calls to put you under 75% coverage.  Thanks again.