Wednesday, August 6, 2014

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

Unit testing is very, very important thing to learn adapt. I would say this is the single most practice in my book which differentiate a good 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 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 unit test for palindrome or prime factor program, but when it comes to write unit test for a feature you are developing which involves database connection, threads, has dependency on other module and don't have a clear entry point, programmer's lost the zeal and enthusiasm. Since user or managers doesn't care about unit tests, programmer really don'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 unit test? How do you decompose a big feature into small units which can be independently testable? Does it sounds like I am talking about Test driven development (TDD)? Well, Yes. Even if you don't follow TDD in it's true sense, it will help you to learn many tricks and techniques to unit test your code. 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 these 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, Java Unit testing 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 make sense to learn tricks in the language you best understand. Since tools and techniques are different for each language, but yes core concept remains same and some techniques are general enough to be utilized in other object oriented languages like C++ or C#.

1. Pragmatic Unit testing in Java with JUnit

Top JUnit Books for Java Programmers
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 has done brilliant work to first introduce concept and process of unit testing before introducing JUnit. Explanation just flows through and you feel you are learning something by following small number of examples given in this book. Yes, book is kinda light on examples, but 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 confirm to 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?

These acronym will help you to write complete test for boundary conditions. Another acronym I like from this book is called A-TRIP which demonstrate properties of 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 unit testing book for Java developers who like to unit test there programs.


2. Test Driven : TDD and Acceptance TDD for Java Developers
Best TDD book in Java
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 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 extremely good job in explaining TDD along with simple technique to write unit tests. As I said before that many Java programmer don'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. 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 infact 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. Good thing is like my favourite book Effective Java, language is simple, explanations are good and examples are non-trivial, which will keep even experienced Java programmers interested.


3. JUnit in Action, Second Edition
Good book to learn Unit testing in Java
Like many similar titled book e.g. Hibernate in Action or Spring in action, this is a comprehensive coverage of both JUnit as 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. 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 reference book now. Petar Tahchiev, Felipe Leme, Vincent Massol, and Gary Gregory have done great job to compiling all useful details about unit testing principles and tools in this book. In one word, use this as 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
Best Books in Test Driven Development and 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 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 behaviour-driven development (BDD) and specification by non-trivial examples, and demonstrate how to add advanced and robust practices into your tool-kit. 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 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 author and book. I wouldn't say this is a must read book, as I have myself not completed it yet, but still a good addition in your book shelf.



5. Practical Java Unit testing using JUnit and Mockito
Java and JUnit Books
Tomek Kaczanowski has done wonderful job in explaining two of the most useful open source testing framework JUnit and Mockito. This book is not just about writing 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 key concept of unit testing e.g. mocks, stubs, parametrized tests and matchers 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. This book also has 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 is very important for any professional Java developer.


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 developers and that to with miles ahead of the crowd, improve your unit testing skill. 5 out of 10 Java developer doesn't know about unit test and out of those 5 only 2 can write high quality, readable and maintainable code. It's one area where senior developer must shine, it's their job to promote best practices, code reviews and unit testing among next generation of Java developers. If you still confused and not sure which Java Unit testing book to buy, go for "Test Driven : TDD and Acceptance TDD for Java Developers", this is THE best book to learn Unit testing for Java programmer.

2 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.

Post a Comment