Mockito Made Clear (Pragmatic Bookshelf)

PragmaticBookshelf
Write effective and useful tests with the Mockito framework. Distinguish between mocks, stubs, and spies, and feel confident about when and where to use each.

Ken Kousen @kenkousen

Edited by Margaret Eldridge @Margaret

Mockito is the most popular framework in the Java world for automating unit testing with dependencies. Learn the Mockito API and how and when to use stubs, mocks, and spies. On a deeper level, discover why the framework does what it does and how it can simplify unit testing in Java. Using Mockito, you’ll be able to isolate the code you want to test from the behavior or state of external dependencies without coding details of the dependency. You’ll gain insights into the Mockito API, save time when unit testing, and have confidence in your Java programs.

If you’ve only ever run a few JUnit tests or injected stubs into classes to return preset values, it’s time to level up your Java toolbox. Dependencies on other classes and external resources can obscure issues and make bugs hard to detect. You need to test classes in isolation to truly pinpoint your problems. And while you could write dummy classes to replace dependencies yourself, Mockito automates the process and helps you fix your code faster.

Start with the Mockito API to generate fake classes for dependencies, configure how each should respond when their methods are called, and verify that the class under test interacts with dependencies the way you’d expect. Next, build unit tests with the Mockito framework and feel confident not just that methods are called, but that they are called the correct number of times and in the correct order. Along the way, follow clear test examples based on JUnit 5 to create stubs, mocks, and spies and find the source of any problems lurking in Java classes.

Save time, write better code, and have more confidence in your Java programs with Mockito.


Ken Kousen is the author of Help Your Boss Help You, the Kotlin Cookbook, Modern Java Recipes, Gradle Recipes for Android, and Making Java Groovy, as well as video courses in Android, Groovy, Gradle, Advanced Java, and Spring. A JavaOne Rock Star, he is also a regular speaker on the No Fluff Just Stuff conference tour and has spoken at conferences all over the world. He has taught software development training courses to thousands of students.

His academic background includes BS degrees in both Mechanical Engineering and Mathematics from M.I.T., an MA and Ph.D. in Aerospace Engineering from Princeton, and an MS in Computer Science from R.P.I. He is currently President of Kousen IT, Inc., based in Connecticut.


Don’t forget you can get 35% off with your Devtalk discount! Just use the coupon code “devtalk.com" at checkout :+1:

Latest Threads About This Book Top

jucosorin
@kenkousen On page 20, in the third code snippet, the following line: PersonRepository mockRepo = new PersonService(mockRepo); should ...
New
jucosorin
@kenkousen On page 25, in the paragraph that starts with “Regardless of whether you use the when method…”, mock should be used instead o...
New
jcbm
@kenkousen On page 14 we define a FakeGateway class that implements our Gateway interface. In the example below we instantiate the Astr...
New
gbouras
Please use the following format for the title of this thread (then simply delete/replace this text with the content for the thread): Tit...
New
lnxrocks
@kenkousen Title: Mockito Made Clear: Chapter 1 (page 2) The first sentence of the first full paragraph on page 2 reads: In we’ll Chap...
New
PragmaticBookshelf
Write effective and useful tests with the Mockito framework. Distinguish between mocks, stubs, and spies, and feel confident about when a...
New

Most Active This Week Top

Most Active This Month Top

Most Active This Year Top

Most Active Last Three Years Top

PragmaticBookshelf
Write effective and useful tests with the Mockito framework. Distinguish between mocks, stubs, and spies, and feel confident about when a...
New
lnxrocks
@kenkousen Title: Mockito Made Clear: Chapter 1 (page 2) The first sentence of the first full paragraph on page 2 reads: In we’ll Chap...
New
gbouras
Please use the following format for the title of this thread (then simply delete/replace this text with the content for the thread): Tit...
New
jcbm
@kenkousen On page 14 we define a FakeGateway class that implements our Gateway interface. In the example below we instantiate the Astr...
New
jucosorin
@kenkousen On page 20, in the third code snippet, the following line: PersonRepository mockRepo = new PersonService(mockRepo); should ...
New
jucosorin
@kenkousen On page 25, in the paragraph that starts with “Regardless of whether you use the when method…”, mock should be used instead o...
New

Most Active Over Three Years Top

Book Info

Publisher: Pragmatic Bookshelf
Tag: #book-mockito-made-clear
Threads: 6

Are you this book's author? To be notified of all posts made about it, sign up an account then click here, then select 'watching' from the bell icon in the top right corner.

Get money off!

The Pragmatic Bookshelf

35% off any eBook

Simply use coupon code "devtalk.com" at checkout. See full details here.