Implementing common logic in base class

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:

  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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật