Currently my project is composed of various concrete classes. Now as I'm getting into unit testing it looks like I'm supposed to create an interface for each and every class (effectively doubling the number of classes in my project)? I happen to be using Google Mock as a mocking framework. See Google Mock CookBook on Interfaces. While before I might have just classes Car
and Engine
, now I would have abstract classes (aka C++ interfaces) Car
and Engine
and then the implementation classes CarImplementation
and EngineImpl
or whatever. This would allow me to stub out Car
's dependency on Engine
.
There are two lines of thought I have come across in researching this:
Only use interfaces when you may have the need for more than one implementation of a given abstraction and/or for use in public APIs, so otherwise don't create interfaces unnecessarily.
Unit tests stubs/mocks often are the "other implementation", and so, yes, you should create intefaces.
When unit testing, should I create an interface for each class in my project? (I'm leaning towards creating interfaces for ease of testing)