OOD Class Principles and Testability
This is the final post in my series on OOD class principles and testability. It has included:
Single Responsibility Principle and Testability
The Interface Segregation Principle and Testability
Dependency Inversion Principle and Testability
Open Closed Principle and Testability
Liskov Substitution Principle and Testability
The Law of Demeter and Testability
There was a pit stop at YAGNI and Testability as it had some bearing on the OCP. It was all kicked of by Benefits of Testability. Which was inspired by Dr. Stefan Jungmayr’s dissertation Improving testability of object oriented system.
BTW Stefan’s site http://www.testability.de/ is a great resource (English Translation).
Here is a table that I think quickly summarizes how, when followed, the principles relate to the three aspects of testability.
| Observability | Controllability | Understandability | |
| Single Responsibility Principle | Yes-less | Yes-less | Yes-less |
| Interface Segregation Principle | Yes-less /w doubles | Yes-less /w doubles | Yes-less /w doubles |
| Dependency Inversion Principle | Yes-less /w doubles | Yes-less /w doubles | Yes-less /w doubles |
| Open Closed Principle | Yes-less /w doubles | Yes-less /w doubles | Yes-less /w doubles |
| Liskov Substitution Principle | No | No | Yes-reuse |
| Law of Demeter | Yes-less | Yes-less | Yes-less |
Yes-less = Because the surface area and or type population of the test subject has been reduced, there is less to: observe, control, or understand.
Yes-less /w doubles = The benefits of Yes-less are gain only by using test doubles (e.g. mock) and interaction based testing.
Yes-reuse = Because test fixtures can be reused to test all subtypes.
After studying the table I noticed two things. It pays to have less to work with in a test. That is to say that when there are very few types and members involved in a test it will be easier to understand. It is easier to observer and control less than more. When you can’t have less it pays to have a test double.
It seems there two general fulcrums that you have to control testability. They are decreasing the surface area and type population or introducing test doubles. As well, they help in increasing the effectiveness of unit tests by eliminating failure points other than the test subject.