I have a class (lets call it A) that:
- In the constructor takes a config and based on it, creates a stub of
a web service and stores a reference to it in a private field. - Has a few methods that call web methods and some stuff inbetween.
I started to create a unit test that:
- Creates an instance of a class A with a dummy configuration.
- Through reflection it injects the mocked web service stub.
Although that web service has plenty of methods.
- Should I mock them all (in every test, with different data)?
- Or maybe I should create another layer that encapsulates only the web methods that are being used?
- Or there is another approach?
You should create a wrapper interface around your webservice, and make your class under test take a dependency on that interface, rather than directly on the webservice; you can then mock the interface. Only make that interface expose the methods of the webservice that you find interesting. This is known as a facade pattern, and is detailed here.
Without having a clue about what you’re testing, aim for something like this:
Then your test would look like this (in this case, using MOQ)