How does everyone else mock web services?

Here’s a situation I ran into a while ago, and will no doubt run into many, many times in the future:

  1. I implement a web service (in .NET 2.0)
  2. I add a web reference in another project
  3. I make calls to the web service
  4. I want to be able to test those calls, and how the client will react to different results

Now, this is an ideal situation for mocks or stubs – except that the proxy code generated by Visual Studio (or rather, wsdl.exe) doesn’t include an interface, which makes it trickier to mock. (I can’t remember whether the methods generated are even virtual or not.) Fortunately, it does write it out as a partial class in VS2005, which makes life a lot easier.

I solved this problem with a home-grown tool, which went through the following phases as a pre-build step:

  1. Check whether the web reference proxy code (specified as a parameter to the tool) has already been modified. (I use a comment at the top of the file to indicate that it’s already been processed.) If it has, stop.
  2. Load the file line by line, using a regular expression (yes, I do use them sometimes) to spot method signature.
  3. Begin a new file with the “I’m modified” comment.
  4. Write out all the old code.
  5. Write out all the method signatures as a new interface.
  6. Write out an extra partial class declaration which just makes the existing class implement the interface.

None of this is too hard – and it could be done with separate files instead of rewriting the generated one, etc. However, I was very surprised when I didn’t find such a tool on the net. (To release one myself I’d probably rewrite it so as to be totally mine rather than my company’s.)

Is there a tool out there which everyone uses and I’ve mysteriously not heard of? (Unlikely.)

Does everyone else take a different approach entirely? (Quite possible.)

Do people just not test their web service calls? (Likely for many people, but not for true TDD-ers, surely.)

13 thoughts on “How does everyone else mock web services?”

  1. I’m sure I’ll start using WCF at some point – but it won’t be for a little while.

    Still, nice to know it handles it. (I really must get a couple of WCF books some time…)

    Jon

  2. I’m not sure what you mean by “the non-regenerated part” – but I’d rather not have to do anything manually, beyond explicitly updating the web reference. The interface should basically be the interface of the web reference – so updating the web reference should update the interface.

    Jon

  3. I didn’t see anything on that page which suggested that an interface is generated for the web reference on the client side though – and that’s what I want to code to. That’s not to say it’s not useful, it’s just not what I was after :)

    I suspect that a shared interface in a common assembly, combined with a manually written partial class declaration for the proxy class which states that it implements the interface is probably the best way to go, to be honest… I might look at moving the current codebase towards that and seeing how it pans out.

    Jon

  4. We typically write a thin wrapper class around the proxy and make that implement an interface we can mock.

    An alternate solution would be to hook into the proxy generation process. I believe we did this on one app, but I don’t have the code handy to tell you more.

  5. We just did this on our project last week and like Jon said in a comment simply made the generated proxy code implement an interface via a small partial class. Fast, simple, easy, and best of all no regular exptrssions.

  6. Yeah but we don’t always want proxy stubs do we.

    i want a baby from across the wire and for it to have a client side cry methord and it’s fine for the service to have default properties but the generated stub does not refect these so when i make a baby client side it has no legs.

    datasets come across the wire with methords and don’t need stubs so why not give us developers more freedom so we don’t need duplicates of a baby class.

    WCF is a let down and is no better then web services.

Comments are closed.