Mockito Made Clear


Mockito Made Clear
by: Ken Kousen (Author)
ASIN: ‎B0BTKPBCQ2
Publisher finelybook 出版社:‏ ‎Pragmatic Bookshelf; (January 24, 2023)
Publication date: ‎January 24, 2023
Language 语言: ‎English
File size: ‎2142 KB
Print length: ‎151 pages

Book Description


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.

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).
From the Preface
This book is about the Mockito testing framework, but it’s not an exhaustive reference. No books these days try to do that. For any library, the online documentation beats printed books every time because it keeps up with new versions of the software and can be adjusted to show new features and bug fixes. Where books become valuable is as a front end to the online docs, in that they help you understand what you’re getting into and what to expect when you get there. Books are curated content. They try to show you what’s important and why.
For Mockito, a book like this one is especially important, because the online documentation contains significant gaps. The docs were written by developers who understand tools like Mockito that generate mocks, stubs, and spies and who help you use them. That’s good, in that the docs focus on exactly how to make the framework do what you want. The downside is that you need to understand the problems that Mockito solves ahead of time, and unless you’ve already had experience with similar tools, those concepts are probably not obvious.
This book is intended to fill that gap. It’s about the concepts behind Mockito more than showing the details of how to use it. Those details are included, of course, for a set of representative examples, but the goal is understanding rather than the specific details of the syntax. The online docs are good for syntax (the classes and methods in the API) and, to a lesser extent, semantics (how to use the tool to do what you want), but they don’t explain why Mockito is useful in the first place or how it can make your job easier.
This is exactly what the Pragmatic Answers series is all about. Answers books cover topics that are too big for a simple blog post but probably not large enough to warrant a 300-page book. Books in this series are short and sweet. The goal is to get you up and running with the tool in a way that you could devour in a weekend if necessary. The end result is to get you started and to help you understand the goals, the strategies, and the basic tactics of the framework. Then you can find any specific details in the online resources and know how to apply them.
Hopefully you’ll find this book a helpful addition to your library and it’ll help you write more productive tests in your Java systems, to be sure your code is doing what you want.
One note about software versions: the current version of Mockito at the time of this writing is 4.0.0. You may be using a version from the 2 line or the 3 line. That’s fine, because these are the only differences:
Mockito 3 is just Mockito 2 with a required Java version of 1.8 or above.
Mockito 4 is just Mockito 3 with deprecated items removed.
Mockito 5 (soon to be released) is just Mockito 4 with the inline mock maker included in the core.
In other words, all the code in this book is written for Mockito 4 but should work on any Mockito version 2.* or above. The Java version will be 11 because even though the current LTS (Long Term Support) version is 17, not many users in the community have moved to that version. All the code in the book will work on all Java versions from 1.8 through 19.
The tests are all based on the JUnit testing framework. Appendix 1, ​Running Mockito Tests​, discusses how to set up a project to include Mockito with both JUnit 4 and JUnit 5, using either the Gradle or Maven build tools, and then how to run the associated tests.
Now it’s time to start looking at the types of problems Mockito is designed to solve.
Amazon page

下载地址 Download解决验证以访问链接!
打赏
未经允许不得转载:finelybook » Mockito Made Clear

评论 抢沙发

觉得文章有用就打赏一下

您的打赏,我们将继续给力更多优质内容

支付宝扫一扫

微信扫一扫