Practical Unit Testing with JUnit and Mockito
Practical Unit Testing with TestNG and Mockito

Nowadays developers are expected to write unit tests. While simple in theory, in practice writing high-quality unit tests can turn out to be a real challenge. This book will help.


This book explains in detail how to implement unit tests using very popular open source Java technologies: JUnit 5, TestNG, Mockito and AssertJ.


It presents a range of techniques necessary to write high quality unit tests – e.g. mocks, parametrized tests and matchers. It also discusses trade-offs related to the choices we have to make when dealing with some real-life code issues.


The book stresses the importance of writing readable and maintainable unit tests, and puts a lot of stress on code quality. It shows how to achieve testable code and to eliminate common mistakes by following the Test Driven Development (TDD) approach. Every topic discussed in the book is illustrated with code examples, and each chapter is accompanied by some exercises.

By reading this book you will:

  • Grasp the role and purpose of unit tests
  • Write high-quality, readable and maintainable unit tests
  • Learn how to use JUnit 5, Mockito, AssertJ, ...
  • Avoid common pitfalls
  • Recognize bad unit tests, and fix them in no time
  • Develop code following the Test Driven Development (TDD) approach
  • Use mocks, stubs and test-spies intelligently
  • Measure the quality of your tests using code coverage and mutation testing
  • Learn how to improve your tests’ code so it is an asset and not a burden
  • Test collections, expected exceptions, time-dependent methods and much more
  • Customize test reports so that they show you what you really need to know
  • Master tools and techniques your team members have never even heard of (priceless!) :)

Read it now!

JUnit 5 version (2nd edition, 2019)

PDF + ePub: Buy PDF + ePub or Buy PDF + ePub from LeanPub



TestNG version

PDF + ePub + Kindle: Buy PDF + ePub + Kindle or Buy PDF + ePub + Kindle from LeanPub


Personal Discounts

If you buy the printed version of any "Practical Unit Testing" book, you can buy ePub + PDF for $10! Just send me a picture of the book on your desk.

Volume Discounts

If you want to buy electronic or paper versions of the Practical Unit Testing book for your team or company, then contact me regarding the final price, please.


Spread the word

Please use the buttons below to inform the world about this book.

It would be awesome if you could write a review and publish it on Amazon or your blog. Thank you!

About the Author

Tomek KaczanowskiHi there!

My name is Tomek Kaczanowski, and I’m happy to present to you the results of my hard work. This book have been written on the basis of my experience as a senior Java developer, trainer, mentor, technical team leader and merciless code reviewer. :)

The book have been proofread by several TDD & Java experts, and corrected by a native speaker, so I feel pretty confident about recommending it. Okay then, let’s forget about modesty: I reckon these really are the best books dealing with unit tests on the market! I hope you’ll share my opinion - when you’ve read them! :)

Personally, I would have been really happy to have had the chance to read book like this some time ago: it would have spared me several grey hairs, for sure! :) I would also be happy if my colleagues were to read them and follow the advice they would find there... :)

Blog Twitter LinkedIn e-mail


Ahh... no matter how many times you check, there are always some bugs! See the errata.

Source code

Source code is available.

Another book, perhaps?

Practical Unit Testing with JUnit and Mockito Practical Unit Testing with TestNG and MockitoBad Tests, Good Tests

Kanban For Your Team 1,2,3 KANBAN - polish version / wersja polska Złe testy, dobre testy