How to mock a web service in .NET

Calling real web service each time you have to run unit tests is not OK. First of all, it makes your tests extremely slow. Secondly, service provider may be unhappy about all these messages you’re flooding their system doing a real webservice call during a unit test run. Actually, they even can charge you for every call you make. Thirdly, webservice may be unavailable due some technical issues which leads your tests to fail (see point 1). Today I’m going to tell you how to remove live web service call from your code.

Basically we have two options:

  • modify your code to use dependency injection and inject Moq object that matches expected interface
  • create a mock Web Service that matches live interface but has a behaviour controlled by you

Let’s choose the second option and mock a web service:

1. Get WSDL you want to mock

Type web service url in your browser and append ?wsdl to the end to get something like this:

Now, save it to your local machine as .wsdl file.

2. Generate code for xml web service

Open Visual Studio Command Prompt and type:

wsdl /language:CS /out:C:\Downloads\ /protocol:SOAP /serverinterface C:\Downloads\webservice.wsdl

This will create cs file in the C:\Downloads directory with number of classes matching the object definitions in wsdl plus one interface that you have to implement to be able to mock your web service.
Note: if you get 'wsdl' is not recognized as an internal or external command eror, type the following command:

cd C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\

3. Create new ASP .NET Web Application

Right click on your solution, select Add->New Project then go to Visual C# -> Web, choose ASP.NET Web Application, type project name and click Ok button:

Then select empty template and click Ok button.
Add the cs file you generated before to the project.
Add a new web service to the project: right click on your ASP .NET project, select Add -> New Item:

Then go to Visual C# -> Web, choose Web Service (ASMX), type web service name and click Add button:

4. Implement the interface in the generated code

Change the class definition in the code for this added web service, so it implements the right interface, e.g replace System.Web.Services.WebService with yours.
Then right click on that interface and select Implement interface:

Visual Studio will generate method stubs so the class matches the interface.
Now you can place your code in the method stubs and define your own behavior.

5. Replace live service call by mocked one

Run ASP .NET application we just created and make a note of the mock service address:

Copy the url of the mocked web service you want to call instead of the real one and paste in your Tests project's app.config file.

That's it folks! Any questions?

Stay tuned. Get post excerpts, news and other perks not included in RSS, Feedly or somewhere else.

Join 258 other followers and receive monthly newsletter

Ivan Grigoryev's Blog
Living in New Zealand. Blogging about the country, beautiful places, everyday life.
Do a skydive - halfway completed; get 1400 - still working on; reach 300kph - completed by 96.6%