Chapter 4. Stubbing Behavior of Mocks

In this chapter, we will cover the following recipes:

  • Using argument matchers for stubbing
  • Stubbing methods that return values
  • Stubbing methods so they throw exceptions
  • Stubbing methods so they return custom answers
  • Stubbing methods so they call real methods
  • Stubbing void methods
  • Stubbing void methods so they throw exceptions
  • Stubbing void methods so they return custom answers
  • Stubbing final methods with PowerMock
  • Stubbing static methods with PowerMock
  • Stubbing object instantiation with PowerMock

Introduction

As explained in the previous chapters, Mockito is all about creating mocks and stubbing their behavior. It's worth taking another look at the differences between mocks and stubs in order to properly distinguish possible ...

Get Mockito Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.