Book Image

Mastering Unit Testing Using Mockito and JUnit

By : Sujoy Acharya
Book Image

Mastering Unit Testing Using Mockito and JUnit

By: Sujoy Acharya

Overview of this book

Table of Contents (17 chapters)
Mastering Unit Testing Using Mockito and JUnit
About the Author
About the Reviewers

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 methods

  • The final methods

  • The static methods

  • The final classes

  • Use 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...