Thursday, February 8, 2018

5 Good Books to Learn Unit testing, JUnit and TDD in Java

Unit testing is a very, very important thing to learn to adapt. I would say this is the single most practice in my book, which differentiates an excellent programmer with a professional programmer. It's one way you can see how disciplined a programmer is? It's also the best way to write clean code; a code which can stand the test of time, a code which is flexible enough to accommodate future changes and a code which you don't afraid while changing. Despite several efforts of promoting unit testing by programming community and emphasizing unit testing by many notable programmers, it's still one of the lacking practice.

In my effort to encourage developers to do unit testing, I try to make it natural rather than a special thing. I have found that it's not that programmer doesn't want to test, or they don't have time, it's because they don't really know how to unit test a non-trivial code. It's easy for someone to write a unit test for palindrome or prime factor program, but when it comes to writing unit test for a feature you are developing which involves database connection, threads, has a dependency on other module and don't have a clear entry point, programmer's lost the zeal and enthusiasm.

Since user or managers don't care about unit tests, the programmer really doesn't push themselves to the level they usually do to make a functional code working. How do we solve this problem? How do you develop that code sense to write a unit test? How do you decompose a big feature into small units which can be independently testable? Does it sound like I am talking about Test-driven development (TDD)? Well, Yes. Even if you don't follow TDD in its true sense, it will help you to learn many tricks and techniques to unit test your code.


On the flip side, It's one area where the senior developer must shine, it's their job to promote best practicescode reviews, and unit testing among the next generation of Java developers.

In this article, I am going to share 5 books, which you would love to read, which will help you to learn unit testing and master the art or writing good tests. Unit testing and JUnit is synonymous in Java world, so most of this book will teach you unit testing using JUnit. This is good because a sound knowledge of how JUnit works, and how you can use different JUnit annotations will encourage you to write more tests.




Top 5 JUnit and TDD Books for Java Programmers

Here is my list of good books to master unit testing, JUnit and Test Driven Development (TDD) for Java professional. Though concept like unit testing is language agnostic, it makes sense to learn tricks in the language you best understand. Since tools and techniques are different for each language, but yes core concept remains the same and some techniques are general enough to be utilized in other object-oriented languages like C++ or C#.


This is the first book I read on unit testing and JUnit, and I still love to read it when I have some free time. It's a short book, but whatever written is absolute gold. Andy Hunt and Dave Thomas have done brilliant work to first introduce the concept and process of unit testing before introducing JUnit.

The explanation just flows through, and you feel you are learning something by following a small number of examples given in this book. Yes, the book is kinda light on examples, but the good thing is that every example was explained very well.

I still remember the CORRECT acronym I learned from this book, which helps you to think tests about boundary conditions. CORRECT stands for
  1. Conformance - does the value conform to the expected format?
  2. Ordering - does value is in expected ordered?
  3. Range - does value is in expected range?
  4. Reference - does code reference anything external which is not in direct control of the code itself?
  5. Existence - does the value exists? (null, non-null, present or absent in Collection)
  6. Cardinality - Are there exactly enough values?
  7. Time (absolute and relative) - Is everything happening in order? At the right time? In time?
This acronym will help you to write a complete test for boundary conditions. Another acronym I like from this book is called A-TRIP, which demonstrates properties of a good unit test. A-TRIP stands for:
  1. Automatic
  2. Thorough
  3. Repeatable
  4. Independent
  5. Professional

By reading this book you will learn how to unit test your Java application, how to write good test, what to test, boundary conditions, dealing with design issues to make your code more testable and testing Gotchas like randomly failing/passing tests, test working on one machine but not on others, tests taking too long to run, etc.

In short, a must-read a unit testing book for Java developers who like to unit tests their programs. If you like online courses you can also combine this with  Java Unit Tests for Beginners course from Udemy to get the best of both worlds.

Top JUnit Books for Java Programmers




I consider myself lucky that I come to know about this book sooner than later. If you ask me to recommend just one book on testing Java application, JUnit, and Test-driven development, this would be the one.

This is THE best book for Java developer, who not only wants to learn JUnit but also learn how to test Java programs, how to test a particular class, method, or module. Lasse Koskela has done an extremely good job in explaining TDD along with simple technique to write unit tests.

As I said before that many Java programmer doesn't write Unit test, not because they don't want or they don't know JUnit but because they lack the skill to come up with test and code that test for a particular unit of their code.

If you prefer online courses, you can also combine this book with Practical Test-Driven Development for Java Programmers to get the best of both worlds.

Best TDD book in Java


Once they know how to treat a piece of code as Unit and how to write a test which is independent, just focus on unit and test simple things, they, in fact, enjoy writing unit tests. Before reading this book, I didn't know why Singleton is anti-pattern and how helpful interface can be to write testable, flexible, and robust code.

Without any doubt, this is the must-read book for any Java developer, and I should have included this in my list of must-read Java Books. Coming back to content, you will learn how to write tests for core Java application, which is the easy part, and also writing tests about Servlet, JSP, Spring MVC Controllers, JDBC classes, tests for Apache Velocity templates and many more.

The good thing is like my favorite book Effective Java, language is simple, explanations are good, and examples are non-trivial, which will keep even experienced Java programmers interested.



Like many similar titled books, e.g. Hibernate in Action or Spring in action, this is comprehensive coverage of both JUnit as the library and testing Java application in general. Apart from introducing JUnit, this book goes beyond that offer useful testing advice on how to use stubs and mocks to test Java application, writing HtmlUnit and Selenium test for Java web application, testing server-side Java code using Cactus, integrating your JUnit test with build tools like ANT, and Maven, automatically running them using CI, etc.

Good book to learn Unit testing in Java

This book is equally useful for both beginners and senior Java developers, you can learn a lot of tools, techniques, and process to test your Java application. I know JUnit even before reading this book, but I have learned a lot and use it as a reference book now.

Petar Tahchiev, Felipe Leme, Vincent Massol, and Gary Gregory have done a great job in compiling all useful details about unit testing principles and tools in this book.

In one word, use this as a reference book to go back in check how to use a particular testing tool, e.g. Selenium, HtmlUnit or Cactus and in general how to test different types of Java applications.



4. Effective JUnit testing: A Guide for Java Developers - Good book to Learn JUnit

This is another gem from Lasse Koskela. By now, You guys should already know hist masterpiece Test Driven, which I have a said a must-read the book for a Java developer who wants to learn unit testing, JUnit, and TDD, but more importantly, want to improve himself to create flexible and robust software.

Effective Unit Testing is written to teach you how to write good tests; tests that are concise and to the point, expressive, useful, and maintainable. Inspired by Roy Osherove's all-time classic The Art of Unit Testing, Effective Unit Testing book focuses on tools and practices specific to the Java world.

It introduces you to emerging techniques like behavior-driven development (BDD) and specification by non-trivial examples and demonstrates how to add advanced and robust practices into your tool-kit.
Best Books in Test Driven Development and JUnit


Unlike previous books, e.g. JUnit in Action, this is a general book on unit testing and teaches you writing better tests, creating testable designs, effective use of test mocks, stubs, and doubles, improving the speed of your test suites and improving readability and maintainability of your tests.

I actually bought this book after reading Test-Driven, because I was very impressed with both the author and the book. I wouldn't say this is a must-read book, as I have myself not completed it yet, but still a good addition to your bookshelf.



5. Practical Java Unit testing using JUnit and Mockito - Best book to learn Mockito 

Tomek Kaczanowski has done a wonderful job in explaining two of the most useful open-source testing framework JUnit and Mockito. This book is not just about writing a unit test but about creating good tests. You will learn how to write readable and maintainable test along with writing high-quality production code.

This book explains the key concept of unit testing, e.g. mocks, stubs, parametrized tests, and matches with some easy to understand but non-trivial examples. Tomek has also included one chapter on developing good code using Test Driven Development (TDD) which will help you understand core concepts in no time.

Java and JUnit Books

This book also has a lot of advanced content for experienced and senior Java developers, e.g. how to improve your existing unit tests, how to measure code quality of your tests using code coverage tools, how to avoid common mistakes while writing unit tests in Java, and using mock, stubs and test doubles cleverly.

It lives up to the expectation and totally justified using "Practical" word in its title by explaining tools, practices, and techniques which are very important for any professional Java developer.

 Further Learning

That's all on my list of Top five books on Unit testing in Java using JUnit, Mockito, TDD and other testing libraries. As I said if you want to become a better developer and that too with miles ahead of the crowd, improve your unit testing skill. 5 out of 10 Java developer doesn't know about the unit test, and out of those 5, only 2 can write high quality, readable and maintainable code.  

P. S. - If you connect better with training courses then books, then here is a list of some good courses to learn JUnit from Udemy and Pluralsight: 5 Courses to learn JUnit and Test Driven Development in Java, check them out.


4 comments :

Anonymous said...

I would highly recommend "Growing Object-Oriented Software, Guided by Tests" (by Steve Freeman and Nat Pryce)

Anonymous said...

You should include Test-Driven Development By Example by Kent Beck, one of the best book on TDD and from the man who is creator of JUnit, if I am not wrong.

Anonymous said...

agreed

meysam amini said...

thanks so much

Post a Comment