6

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
  • 2
    recommended reading: Discuss this ${blog}
    – gnat
    Mar 25 '15 at 14:41
  • 1
    Blanket statements like that suggest to me that someone found a herd to go trotting after Mar 25 '15 at 14:43
  • 3
    I think there's a good question in here somewhere. Can you edit the question & perhaps substitute what you have in your comment "How can one implement a common logic..."? Mar 25 '15 at 14:57
  • 1
    @DanPichelman Edited. Thanks for understanding. If you think of a better way to edit it please do I'm terrible at expressing myself ...
    – Omribitan
    Mar 25 '15 at 15:00
  • 1
    @AK_ Pretty sure the Agile school of thought is "the test suite should run constantly and very fast. If your tests run slow, that's a problem."
    – Andres F.
    Mar 25 '15 at 18:29
2

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
  • 4
    I strongly disagree that well designed OO code is harder to test. It is, to the contrary, easier to test. If you have a private method in a class, it's quite likely that it should actually be a public method on a different class (making it easy to test), which is then a private member variable contained in the original class.
    – Rob K
    Mar 25 '15 at 17:49
  • 2
    couldn't disagree more. A good Object Oriented Design should also be a very very testable design. The principles of good design, OO or not, are a super-set of the principles that make software testable.
    – AK_
    Mar 25 '15 at 17:54
  • If we put aside the discussion about the conflict between TDD and OOP, DI pointed me in the right direction so I'll mark this as answer
    – Omribitan
    Mar 29 '15 at 13:08
  • @Ryathal TDD is definitely overrated and overpromoted, but it would be beneficial if you could provide a small code example to confirm your opinion.
    – Den
    Mar 30 '15 at 11:10
3

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
  • I like the comparision to private methods. so lets say I have a class with a private method that takes long time to execute. however, when I'm testing my public methods I simply want it to always return 'true' since there is no actual need for that long runnning operation. How can I achieve that?
    – Omribitan
    Mar 25 '15 at 16:39
  • 1
    Long-running to me usually means you have crossed an application seam and stepped into the realm of I/O bound modules such as data or file access, network, etc. Crossing that boundary means it should be in another, easily mockable class, not in a private method. Mar 25 '15 at 16:45
  • As long as you stay in-memory and not I/O bound I'd say test it without mocking it, or if it's really too slow, put the test in a long-running test suite that is executed less frequently. Mar 25 '15 at 16:47
3

Absolute nonsense.

A few thoughts:

  1. 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.

  2. Testing, and specifically Unit Testing is meant to serve the project, verify it's design, and assure its robustness not the other way around.

  3. 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:

  1. 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.

  2. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.