O'Reilly logo

Test-Driven Java Development - Second Edition by Viktor Farcic, Alex Garcia

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 with 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required