Jump to ratings and reviews
Rate this book

Pragmatic Unit Testing in Java with JUnit

Rate this book
Learn how to improve your Java coding skills using unit testing. Despite it's name, unit testing is really a coding technique, not a testing technique. Unit testing is done by programmers, for programmers. It's primarily for our benefit: we get improved confidence in our code, better ability to make deadlines, less time spent in the debugger, and less time beating on the code to make it work correctly.

This book shows how to write tests, but more importantly, it goes where other books fear to tread and gives you concrete advice and examples of what to test--the common things that go wrong in all of our programs. Discover the tricky hiding places where bugs breed, and how to catch them using the freely available JUnit framework. It's easy to learn how to think of all the things in your code that are likely to break. We'll show you how with helpful mnemonics, summarized in a handy tip sheet (also available from our pragmaticprogrammer.com website) to help you remember all this stuff.

With this book you will:



Write better code, and take less time to write it Discover the tricky places where bugs breed Learn how to think of all the things that could go wrong Test individual pieces of code without having to include the whole project Test effectively with the whole team
We'll also cover how to use Mock Objects for testing, how to write high quality test code, and how to use unit testing to improve your design skills. We'll show you frequent "gotchas"--along with the fixes--to save you time when problems come up. We'll show you how with helpful mnemonics, summarized in a handy tip sheet (also available from our pragmaticprogrammer.com website).But the best part is that you don't need a sweeping mandate to change your whole team or your whole company. You don't need to adopt Extreme Programming or Test-Driven Development, or change your development process in order to reap the proven benefits of unit testing. You can start unit testing, the pragmatic way, right away.

176 pages, Paperback

First published September 1, 2003

35 people are currently reading
323 people want to read

About the author

Andy Hunt

23 books405 followers
see also Andrew Hunt

Andy Hunt is a programmer turned consultant, author and publisher.
He co-authored the best-selling book "The Pragmatic Programmer",
was one of the 17 founders of the Agile Alliance, and co-founded
the Pragmatic Bookshelf, publishing award-winning and critically
acclaimed books for software developers.

Andy started writing software professionally in early 80's across
diverse industries such as telecommunications, banking, financial
services, utilities, medical imaging, graphic arts, and of course,
the now-ubiquitous web.

Source: Amazon.com

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
52 (18%)
4 stars
110 (40%)
3 stars
84 (30%)
2 stars
22 (8%)
1 star
6 (2%)
Displaying 1 - 21 of 21 reviews
Profile Image for Rod Hilton.
152 reviews3,116 followers
November 16, 2015
Full disclosure: I was a technical reviewer for this book

This is just a simple but rock-solid book on unit testing in Java 8. I wouldn't let the title fool you - it doesn't seem to be a big Java 8 book, in fact it doesn't really even seem to leverage the features Java 8 introduces very much. I think mostly it's a book about Unit Testing in Java, and Java 8 happened to have been released at the time, so it got thrown in the title. The code definitely IS Java 8 (not 7), but I wouldn't say that it's a book all about how Java 8 introduces new features, and here's how to unit test code that uses them, which is kind of what the title implied to me.

Mostly the book seems geared to people who are brand new to unit testing, maybe people who had never written a unit test before. The different mneumonic devices (CORRECT, FIRST, etc) underscore the targeting of newbies. However, it does NOT seem like it is a book for people new to Java itself, or even Java 8, since very little explanation is provided for any of the code aside from the unit tests themselves and why they are the way they are. If you're a big unit tester, you might find a lot of the book doesn't provide you with anything new, but honestly I actually recommend reading it anyway.

Part I is going to be familiar to anyone with background unit testing - it's just the basic mechanics of testing itself. Part II however might give even unit testing veterans some stuff to think about. It's basically all about how to write effective tests that really stress the system - and it introduces a number of mnemonics to that end. A little cheesy but I actually learned quite a bit here, and was surprised at how useful I found it, given how much experience I have writing tests.

When I first started reading Part III, all about the overall design of your code, my reaction was thinking, gosh, a lot of other books cover refactoring... it seems out of scope for a book on testing. I was very skeptical, and I didn't understand why it was included. But once I kept reading, I saw the point, and saw how using mock objects were going to allow us to refactor the book's primary example code into something more testable, and better-designed because of it (it even fixed an issue I had with the code when I first saw it, the side effects in the scoring method). By the end of the section, I was fully on board, and to be honest an entire chapter about refactoring tests was simply amazing (and easily my favorite chapter). To be frank, these later chapters are worth the price of admission alone for the book, I can't remember any other single book that covers refactoring to improve quality of tests themselves (especially in a step-by-step fashion). I can't even remember a book that covered testing multithreaded code, and for it to just be a chapter at the end of a book on testing in general was a real treat. This book covered absolutely everything, mock objects, refactoring, TDD, SOLID, and of course the basic principles of unit testing in general.

I was pretty impressed with how thorough and useful parts I and II were, but I was blown away by III and IV. Simply put, this is probably my favorite book on unit testing I've ever read. If you're new to unit testing and use Java (even if it's not Java 8), this book is a must-read. If you're a unit testing pro, there might not be that much for you here, but I still recommend giving it a read anyway and just skipping bits and pieces. Parts II and III in particular have a lot of valuable insight to share.
Profile Image for Bernie Noel.
20 reviews13 followers
Read
December 24, 2019
The book is awesome. I felt like its repteitive in some areas to help a novice programmer to understand how important is the NOTION but its a 10/10 book. A must read for being a better coder.
Profile Image for Anish K George.
69 reviews1 follower
February 29, 2016
It is good book for the very basics. For a junior developer, who is starting to write programs, reading this book will help to create a correct perspective towards unit testing.

But if you are experienced successful programmer already I would suggest to avoid this book, since this book is seems to contain very old principles and lacking the information about the latest tools or frameworks for Unit testing
Profile Image for Henggao Cai.
19 reviews4 followers
January 9, 2022
I highly recommend this book for anybody who wants to get started in the world of automation testing. This book is also a must read for developers. We really don’t write enough Unit tests.
Profile Image for Jeanne Boyarsky.
Author 28 books76 followers
September 10, 2016
“Pragmatic Unit Testing in Java 8 with JUnit” covers JUnit 4 and a mix of Java 7 and 8. JUnit 5 wasn't out yet when the book was written so the decision to cover JUnit 4 makes sense. There was good coverage of assertThat along with three stycles of exception testing.

There were some Java 8 idioms used include functional interfaces, method references, streams and lambdas. There were also some places that could have used lambads and didn't. For example, chapter 8 makes a big point of refactoring to make the code cleaner. But then the anyMatches/matches methods use a loop.

The narrative about Dale testing and Pat the skeptic added nice color. Each chapter ends with a short “After” section that provides a mix of summary and transition. I also liked the technique of using arrows to show what changed in each refactoring and edition. Testing concepts were covered well including the FAST, Right BIPCEP and CORRECT acronyms.

I think the book was great. It's a timely updated to the original Pragmatic Unit Testing book (which uses JUnit 3.8.) But I have to take off a tiny bit for the fact that Java 8 looks to have been retrofitted in.

I give this book 9 out of 10 horseshoes.

Disclosure: I received a review copy of this book from the publisher for reviewing it on behalf of CodeRanch.
Profile Image for Erika RS.
849 reviews259 followers
January 24, 2013
Pragmatic Unit Testing is a book about testing for developers. The content is appropriate for developers who do not have much experience testing and for developers who do unit test their code but do not have formal training in testing.

The book describes the purpose of unit tests, how to write them, and what makes a good unit test. They give lots of examples to help developers figure out what kind of things are good to test and how to test them. Although a lot of the information can be found in general software engineering and development books, Pragmatic Unit Testing provides a concise, well explained introduction to developer testing.

I have a more complete summary here.
Profile Image for Ajita Gupta.
96 reviews
March 9, 2022
This book is not written for beginners. I couldn't follow the examples. Nor did I understand much of the code snippets. Mock objects and stubs are not explained well either. Look elsewhere!
Profile Image for Jean Tessier.
163 reviews32 followers
Read
October 3, 2011
Very good intro to unit testing. Talks at great length about the need for tests and the advantages of designing for testability. It has a very nice tutorial on using JUnit and even briefly mentions the use of mock objects.

It is filled with very good pieces of advice. Many of them are arranged into catchy mnemonics. But they often feel forced and some are so long that it becomes hard to recall what they stand for. This reminded me of a similar device that I keep hearing: DRY - Don't Repeat Yourself. It's about removing code duplication, but everytime I see "DRY", it takes me a while to remember what it's about. So I try not to rely on mnemonics too much.

Pretty much everything it says I had already gathered from various other sources. Martin Fowler's books, Software Development magazine, Bob Martin's writings, etc. It is nice to have it all neatly summarized in one place, but it was all a little too entry-level for me. I would definitely recommend it to anyone who was not test-infected yet, though.

Very much like the authors' other book, The Pragmatic Programmer. I think DRY more or less originated in that book, actually.
Profile Image for David Workman.
22 reviews12 followers
July 12, 2010
A worthwhile read for someone looking for a solid introduction to both JUnit and unit testing in general.

This book focuses on practicalities, giving a solid basis and solid reasons for unit testing as a development practice. It makes it clear that unit testing should not be the only form of testing, but should be the lowest foundation upon which a solid testing plan is built.

The text is interspersed with examples and exercises for the reader to complete and appendixes provide a guide to installing JUnit and a summary of the important topics of the book as a quick reference. The writing style is accessible and easy to read with the anecdotes and narrative of the book being very similar to 'The Pragmatic Programmer' also by the same authors.

Overall, this book is a solid introduction to unit testing in both practicalities and underlying reasons. I would definitely recommend this to a java developer looking to start out in unit testing and is looking for an introductory text.
34 reviews
October 5, 2016
This is a new edition covering JUnit 4 with Java 8. It is a pleasant introduction to JUnit and you'll get a solid grasp of unit testing, but it kind of stops there. For deeper insights of how to use, for example, JUnit Rules, you'll need to go to official documentation or Google for their usage in the wild.

For some unknown reason, it uses new Java 8 features (like stream sorting with lambda filters and predicates) in it's examples. Those might be a hindrance if you still haven't learned and started using them.

It would of been nice if the book went a bit further than just an introduction to the unit testing. "Growing Object-Oriented Software Guided by Tests" might be a nice followup to this book.
Profile Image for Diego.
171 reviews
January 29, 2013
Este libro pertenece a una serie de títulos en donde se van exponiendo varias herramientas de programación; en este tomo, se revisa JUnit que es un framework de pruebas para Java.

Además de mostrar de manera concisa cómo se usa JUnit, se exponen varios consejos y aspectos del testing; de hecho, la mayor parte del libro trata de eso. En algunas partes, se pierde la continuidad de la exposición debido a la revisión de los argumentos, aunque nada grave si se vuelve al punto de partida del tema visto.

Este libro es ideal para introducirse al tema de testing en programación.
Profile Image for David Rissato Cruz.
25 reviews6 followers
October 20, 2016
This book isn't exactly about Unit Testing in "Java 8" but more about "Unit testing using JUnit (and occasionally some Java 8)". Apart of that, It is an excelent resource on good practices about unit testing and it is very well organized. Set your expectations before reading it (about the non-intensive Java 8 part) and you will find it worthwhile.
Profile Image for Jonathan.
248 reviews
March 27, 2009
This book is another great book from the pragmatic programmers to help people get started with unit tests using junit. If you are already familiar with junit and writing unit tests, then there are a few tidbits of good information. This book is definitly more for beginners.
Profile Image for Bartek Piech.
26 reviews3 followers
January 5, 2015
Simple and shallow introduction to unit testing. Covers JUnit, Hamcrest and Mockito briefly. Touches such topics as design, SOLID, refactoring and TDD. Overall it's good introduction to unit testing.
Profile Image for Madhur Ahuja.
161 reviews11 followers
April 3, 2016
Verify short book on Java unit testing. Suitable for juniors and college goers. Doesn't contain advanced material such as how to mock database, network calls.
Profile Image for Mohammad.
5 reviews
November 1, 2016
Good book to read about unit testing. Mostly theoretical, but based on JUnit. I do not recommend this book for newcomers to unit testing.
2 reviews
July 30, 2023
Good introduction to unit testing concepts, not much meat on the bone.
Displaying 1 - 21 of 21 reviews

Can't find what you're looking for?

Get help and learn more about the design.