JUnit in Action
by Vincent Massol
August 02, 2006
Okay, so this book is a couple of years old and it was written before JUnit 4.x was released. However, I find the discussion on how to approach unit-testing for non-trivial applications such as those dependant on JEE container or database services to be still conceptually relevant today. Despite its many examples the book is easy to follow and the amount of detail is not overwhelming.
For Vincent Massol, this book was a culmination of 4 years of research and practice in the testing field. His interest in writing tools to help developers write better code has led him to become involed in open-source projects related to software quality. Vincent is the founder of Jakarta Cactus and is a very active participant in the Maven community.
Part 1 of the book introduces JUnit and shows us how to run it with Ant, Maven, and Eclipse. The remaining 2 parts cover unit-testing for non-trivial code such as servlets, JSP's, EJB's, as well database access code. For the most part the book relies on Cactus for in-container unit-testing. However in no way does it suggest in-container unit-testing for all code that may run inside a JEE container or connect to a database.
Chapter 4 is where we get introduced to the 3 flavors of unit-testing - logical, integration, and functional (the latter should not be confused with regular functional testing). Throughout the remaining chapters we see how these three types drive the unit-testing of code and especially non-trivial one.
A recurring theme throughout the book is that regardless of the layer being tested, a good strategy would be to separate business logic from integration code (code interracting with the JEE container or accessing a database) and then: (a) test business logic with fine-grained, logical unit tests and (b) test integration code with integration unit-tests (i.e. using the in-container approach and allowing connections to a real database).
The book also presents typical refactoring techniques required to make classes more testable. The use of mock objects for example tends to lead to more refactoring than say the in-container testing. However, very frequently such refactoring improves the design of the classes under test.
Vincent Massol brings a lot of first-hand experience to the writing of this book. In his blog today he continues to share unit-testing tips and techniques.