Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • MOCKITO COOKBOOK
  • Toc
  • feedback
MOCKITO COOKBOOK

MOCKITO COOKBOOK

By : Grzejszczak
4.3 (4)
close
MOCKITO COOKBOOK

MOCKITO COOKBOOK

4.3 (4)
By: Grzejszczak

Overview of this book

This is a focused guide with lots of practical recipes with presentations of business issues and presentation of the whole test of the system. This book shows the use of Mockito's popular unit testing frameworks such as JUnit, PowerMock, TestNG, and so on. If you are a software developer with no testing experience (especially with Mockito) and you want to start using Mockito in the most efficient way then this book is for you. This book assumes that you have a good knowledge level and understanding of Java-based unit testing frameworks.
Table of Contents (12 chapters)
close
11
Index

Stubbing void methods


In this recipe, we will stub a void method that doesn't return a value. The trick with void methods is that Mockito assumes that they do nothing by default, so there is no need to explicitly stub them (although you may do it).

How to do it...

If you do not want your void method to execute logic, you need to perform the following steps:

  1. Do nothing: Mockito stubs the method for you so that it does nothing.

  2. Explicitly tell Mockito that the void method does nothing; for the BDD approach, call BDDMockito.willNothing().given(mock).methodToStub(), or in the standard way, call Mockito.doNothing().when(mock).methodToStub().

  3. Regardless of the chosen approach in the given(...) or when(...) method, you have to provide the mock object (and not the method call in the case of methods that return values).

  4. Remember that the last passed value during the stubbing will be returned for each stubbed method call. In other words, say that you stub the mock as follows (the willThrow answer will be...

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete