views:

54

answers:

2

Suppose ConcreteService is the only non-testing implementation I'll need. Is it ok to extract an interface (or make its methods/properties virtual) solely for the purpose of being able to test objects dependent on it? Else I'll have to new up the dependency line, up until it branches (at which point I can inject mocks) every time I want to test something dependent on it.

+2  A: 

This is perfectly ok.

I'd go as far as saying that even if you were not doing testing there can be a lot of benefit in introducing the interfaces early on, and it is often prefered.

By coding to interfaces early in the life of a project your code that is calling the ConcreteService can call the interface instead and then if you ever want to introduce a new implementation of the service you've reduced the amount of change required to use it.

David Hall
+1  A: 

Not exactly sure what the design is. Sounds like there are needs to test against the abstraction anyways, then it makes sense to interface it.

Dr. Xray