Designing for testability
We learned about testing impediments and how to refactor them. We cannot unit test code when testing impediments are present; we refactor the code and move the impediments out (to another class or methods), and during testing, the impediments are replaced with mock objects.
However, sometimes we cannot mock out the external dependencies because of testing an unfriendly design. This section covers the design for testability, or rather matters to avoid in code. The following Java constructs go up against mocking the testing impediments:
Constructors initialize testing impediments
Class-level variable declaration and initialization
The
private
methodsThe
final
methodsThe
static
methodsThe
final
classesUse of
new
Static variable declaration and initialization
Static initialization blocks
You cannot unit test the legacy code because either it is tightly coupled or testing unfavorable language constructs hide the testing impediments. The following section explains the testing...