Background
In the documentation of a project I’m working on I came across the following sentence which immediately triggered an alarm for me:
when having several concrete classes that inherits from the same base class, logic which is common for all sub classes should NOT be implemented in the base class.
This is a pretty bold statement which kind of conflicts with everything I think of OOP.
The reason stated was testability. It said that when writing unit tests, if a common logic is implemented in the base class, it will be tested multiple times for each concrete sub class and cannot be mocked out.
Question
Where or how can one implement a common logic for multiple sub classes so it can be easily mocked when they are being tested?
13
You stumbled on one of the Bigger problems that tends to get swept under the rug in unit testing/TDD discussions. Well designed code from an object oriented perspective is generally hard to unit test, code that is easy to write unit tests for usually is compromising some paradigms of object oriented design.
Most approaches to unit testing tend to drive towards a design that makes unit testing easier, which isn’t bad depending on what rules you bend/break. These designs tend to over expose methods so they can be more easily isolated, using interfaces or utility classes to handle common code.
Dependency injection is one of the more popular ways to handle these situations. Here is a pretty simple example in C#. Essentially rather than inheriting directly from a base class, each class will instead implement an interface that is dependent upon an object that handles the common operations. This way you can simply pass MyMockedCommonClass instead of MyCommonClass in your unit tests. This also allows your code to still be pretty Object Oriented friendly.
4
If that common behavior sees frequent use, it will admittedly be executed multiple times during tests for each of the subclasses, but so will it at runtime in production, there’s nothing wrong with it. However, that doesn’t mean you should specifically test the common logic multiple times.
Concrete/abstract superclass
If the superclass is not abstract and the behavior public, you should have one test for it. Plain and simple. No need to repeat the test for each child.
What I often found though was that you mostly don’t have concrete superclasses but abstract ones. Common behavior contained in an abstract class isn’t tested per se but as part of the derived classes tests. I don’t see it as a problem, it’s much like when a private method is tested indirectly as part of a public method test.
Mocking
You shouldn’t need to mock or stub out common behavior from a base class (or from one of your own private methods, which is exactly the same), because what belongs to your superclass also belongs to you, and trying to mock yourself is stupid.
If you feel that need, it’s probably that your class is not cohesive enough and you should extract that behavior to a separate unrelated class instead of using inheritance.
3
Absolute nonsense.
A few thoughts:
-
You should not take OOP and it’s principles too seriously. Many times functional or other software paradigms make more sense. Though the objects thing is cool… thinking in concrete components and all that.
-
Testing, and specifically Unit Testing is meant to serve the project, verify it’s design, and assure its robustness not the other way around.
-
Composition is almost always better than inheritance
by the way, this principle also plays nicely with OOP, the huge OO
inheritance trees from the 90’s were really just a bad
interpretation of OOP. I also suggest you look up the original Gang
of Four book on Design Patterns
To your question:
If several classes are essentially the same “thing” and they have a lot of common functionality you should make them inherit from the same class (assuming you are using an OO programming language of course…).
This in no way limits testability.
To test the parent class you create a subclass for the sake of the test (put it next to the tests class). this is exactly how the class is intended to be used, and what it’s design dictates.
To test the children you can do two things:
-
The bad approach. this applies only to dynamic languages, C#, and maybe Java. you can create a Shim to pose as the supper-class and use it as a base class for the child.
-
Accept that the super-class is an essential part of what the sub-class is. By design. It must conform to behaviors defined in the supper class, and that you actually should test them together. A good example for these are the Strategy and Template Method design patterns. Now you can simply test the subclass as is. you also should refrain from testing the base class too much, because you already done it somewhere else.