Book Image

Test-Driven Java Development

Book Image

Test-Driven Java Development

Overview of this book

Table of Contents (17 chapters)
Test-Driven Java Development
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
8
Refactoring Legacy Code – Making it Young Again
Index

Mockito


Mockito is a mocking framework with a clean and simple API. Tests produced with Mockito are readable, easy-to-write, and intuitive. It contains three major static methods:

  • mock(): This is used to create mocks. Optionally, we can specify how those mocks behave with when() and given().

  • spy(): This can be used for partial mocking. Spied objects invoke real methods unless we specify otherwise. As which mock(), behavior can be set for every public or protected method (excluding static). The major difference is that mock() creates a fake of the whole object, while spy() uses the real object.

  • verify(): This is used to check whether methods were called with given arguments. It is a form of assert.

We'll go deeper into Mockito once we start coding our Tic-Tac-Toe v2 application. First, however , let us quickly go through a new set of requirements.