About This Title

Pages: 87
Published: January 2023
ISBN: 9781680509670
In Print

Skill Level Meter

Mockito Made Clear

Java Unit Testing with Mocks, Stubs, and Spies

by Ken Kousen

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.


About the Pragmatic Answers Series

Sometimes “right now” just isn’t soon enough. When you’re facing a hard deadline and need features that work and code that passes all the tests, there’s no time to hunt through forums and dubious blog posts hoping to stumble across a solution. You need accurate information from experts you trust. You need hands-on instruction that gets right to the point. You need Pragmatic Answers.

Ebook only and short (around 50 pages), the Pragmatic Answers Series gets you over the toughest hurdles in the blink of an eye.

eBook Formats:

  • PDF for desktop/tablets

  • epub for Apple Books, e-readers

  • mobi for Kindle readers

Get all eBook formats here for $9.99 (USD)

Add to Cart we accept visa, mastercard, amex, discover, paypal


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.

What You Need

In this book, we use Mockito 4, the latest version of the Mockito framework, but the concepts and the code works on versions 2 and 3. The tests are based on JUnit 5 (known as Jupiter). There are some differences in JUnit 4, but none of those affect Mockito directly. The underlying Java version is 11, but the provided tests in the GitHub repository will work successfully for any version of Java above that (including the current Long Term Support version, 17).

Resources

Releases:

  • P1.0 2023/01/23
  • B2.0 2022/09/28
  • B1.0 2022/05/31

Contents & Extracts

  • Foreword to Mockito Made Clear
  • Acknowledgments
  • Preface
  • Build a Testing Foundation
    • Saying Hello to Mockito
    • Counting Astronauts by Spaceship excerpt
    • Creating the Basic Classes
    • Adding Unit and Integration (End-to-End) Tests
    • Wrapping Up
  • Work with the Mockito API
    • Selecting Our System to Test
    • Creating Mocks and Stubs
    • Setting Expectations
    • Using Mocks and Stubs in the Astro Project excerpt
    • Wrapping Up
  • Use Built-in and Custom Matchers
    • Using the Existing Argument Matchers
    • Creating Custom Argument Matchers excerpt
    • Verifying the Order of Methods Called
    • Wrapping Up
  • Solve Problems with Mockito
    • Deciding Between Mockito and BDDMockito
    • Testing void Methods Using Interactions
    • Capturing Arguments
    • Setting Outputs Based on Inputs with Custom Answers
    • Spying to Verify Interactions
    • Wrapping Up
  • Use Mockito in Special Cases
    • Mocking Final Classes and Methods
    • Using Mockito with Android
    • Mocking Static Methods
    • Mocking Constructors
    • Working with the Spring Framework
    • Deciding When Not to Use Mockito
    • Wrapping Up
  • Running Mockito Tests
    • Steps Common to All JUnit Versions
    • JUnit 5
    • JUnit 4

Author

Ken Kousen is the author of the Help Your Boss Help You, 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.

eBook Formats:

  • PDF for desktop/tablets

  • epub for Apple Books, e-readers

  • mobi for Kindle readers

Get all eBook formats here for $9.99 (USD)

Add to Cart we accept visa, mastercard, amex, discover, paypal

Related Titles:

Skill Level Meter

About This Title

Pages: 87
Published: January 2023
ISBN: 9781680509670
Edition: 1
In Print