Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing strategy when working with external webservices

I often have to code against Twitter, and now against other Web Service APIs that I do not control.

I've tried to write test cases but it seems too difficult as:

  1. I am bound to the speed of something I cannot control (the web-service).
  2. I cannot control the state of the web service (cannot delete an account, tweet, and restart)

For folks facing similar situations, I'd like to find out the solutions they've devised.

PS: I'm familiar with mock objects but I'd like to get more specific lessons learned -- for example I use a library to talk to Twitter (Twitter4J) which, when last I looked does not allow me to substitute a mock object for the Twitter Service.

Thanks.

like image 713
user1172468 Avatar asked Apr 28 '26 15:04

user1172468


1 Answers

In my projects (which rely on a variety of external web services) I've focused my testing on failure events. So when building a unit test for Facebook Connect I tried to pass junk data to my web service response processing methods to make sure that I could handle the service changing underneath me. Successful event testing wasn't something I put into a unit test because it creates so much cruft (like events in a timeline). A possible work around to this are to create a test account in your intended service and point your unit tests to that ID. If you make sure that your web service posting code doesn't include hard coded IDs (I like to create <Service>Config classes that are passed to <Service>Methods classes like FacebookMethods.auth(FacebookConfig config, String uid)) this lets me create a TestConfig for operations that will modify a service in a way that can't be undone or shouldn't be seen publicly).

like image 66
Jason Sperske Avatar answered May 01 '26 05:05

Jason Sperske



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!