Book Image

Mockito Essentials

By : Sujoy Acharya
Book Image

Mockito Essentials

By: Sujoy Acharya

Overview of this book

Table of Contents (14 chapters)

Designing for testability with Mockito

We learned about testing impediments and how to refactor them. We cannot unit test code when testing impediments are present; we refactor code and move the impediments out (to another class or method), and during testing, the impediments are replaced with mock objects. PowerMock is a dirty solution and it should only be used for legacy code. This is the better way is to refactor the source and make more test friendly.

However, sometimes we cannot mock out the external dependencies due to testing-unfriendly design. This section covers the design for testability, or rather, things 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

  • Private methods

  • Final methods

  • Static methods

  • Final classes

  • Use of new

  • Static variable declaration and initialization

  • Static initialization blocks

You cannot unit test legacy code because it is either...