David Sills's muse is fueled by Bach, Beethoven, and Brahms (and Berlioz and Boulez). He has no life, which comports well with the über-geekitude to which he aspires. He reads and writes fitfully in English, Spanish, French, German, Italian (mostly medieval), C, VB, Perl.... Oh, and Java and XSLT, lots and lots of Java and XSLT. Trained somewhere in darkest Central America, he works today at DataSource, Inc., on the product team for the CASE tool Jetson.
| Author(s) | Cédric Beust, Hani Suileiman |
|---|---|
| Publisher | Addison Wesley |
| PubDate | 2008 |
| Reviewer | David Sills |
![]() |
Purchase and help Javalobby |
|
Positives
Negatives
|
Next Generation Java Testing is intended as an comprehensive introduction to the TestNG (pronounced "testing", but derived, one understands, from "Testing, the Next Generation"). The book lives up to its lofty intention: it is hard to imagine the relevant subject not touched upon in this text.
The audience for the book is Java developers currently using TestNG and those unsatisfied with their current toolkits for unit and other tests. TestNG is built around different principles from the ubiquitous JUnit, and some of its features are certainly among those devoutly wished for by anyone who has tried to modularize a test in that popular framework. For its audience, this book is surely a must-have.
As the first book on the subject of TestNG, Next Generation Java Testing is properly authored by real experts on the framework: the original developer (Cédric) and a major contributor (Hani). They cover a wide range of topics, some only peripherally having to do with TestNG, but all relevant to the issue of testing and to the real-life experience of the authors, much of which is clearly hard-won. Along the way they discuss ways to design code to make it more testable, common coding errors that result not only in ineffective tests but in confusing and hard-to-track errors in production, and particular problems that arise when testing enterprise applications, especially applications that depend upon Java EE container support.
Disclaimer: I am a huge Cédric Beust fan, having frequently used EJBGen, his brilliant framework for generating EJB 2.0 artifacts.
That said, I fear I am going to be a disappointment to Cédric in reviewing his new book. TestNG may well be the best thing since sliced bread and as brilliant as EJBGen ever was (I certainly hope so for the cause of testing, which I very much hope it will forward). Unfortunately, the authors have so often resorted to diatribe rather than positive assertions and first-class examples that I came away from the text much better acquainted with what they didn't like about JUnit (and several other frameworks) than I did with a reason to be excited by TestNG and to promote it in my own development.
There are examples, of course, throughout Next Generation Java Testing. Unfortunately, many are incomplete, even vague; they seem to suggest that only readers with the ability to complete them are worthy to own and use the book. Those examples that are presented more completely are still too trivial, however, and there is no thread among the examples to tie them together. Yes, the very experienced developer can argue from the examples to real-life cases he or she has encountered; but the sense here is that the authors didn't put together a consistent set of examples because they didn't regard them as particularly important. (Consistent with this, no download of examples from the book seems to be available.) Many will be disappointed by the lack of anything they can take home and try out, even in miniature. The pressures on developers are simply too great to allow them leisure for the kind of learning curve the authors have put before them.
I do not for a moment fault TestNG or its designers; the framework decidedly has a place in the arsenal of developers whose particular needs it fits, and I don't doubt that these will be many. It is therefore doubly unfortunate that so negative a tone pervades this first introduction to the framework in the marketplace. It will put off readers with an interest in positive expositions of the fruits of the kind of vast experience these authors have had. They have, no doubt, such a book in them. This, lamentably, is not it.
| Relevance | |
|---|---|
| Readability | |
| Overall | |
Chapter 1. Getting Started
This chapter begins with a critique of issues the authors perceive with JUnit 3. This is followed by a section on designing code to make it more testable. In particular, the "extreme encapsulation" of object-oriented programming and a perceived emphasis on static methods and fields by the design patterns movement are taken to task for making testing unnecessarily difficult. Refactoring code and using dependency injection are recommended solutions to making code more testable. Finally, the TestNG framework itself, the principal subject of the book, is introduced.
Chapter 2. Testing Design Patterns
The authors put forward a number of common testing circumstances and TestNG's solutions to the issues these present. From testing for failure (certainly something as important as testing for success) to multi-threaded or asynchronous testing to the use of mocks and stubs, their presentation of the inherent problems in testing in these scenarios is often well-taken; TestNG certainly seems to have been designed to meet specific needs in those scenarios. Discussion of some well-known code coverage tools and advice about how to they might be best used closes the chapter.
Chapter 3. Enterprise Testing
The problem of testing enterprise Java code is more complex, such code generally having more moving parts and often interacting with legacy systems that cannot be significantly altered. Here integration and functional testing is presented as more appropriate than unit testing, at least in cases where tests must be applied to systems that have already been partially developed without tests. This and the next chapter cover the difficulties of working with Java EE specifications, and with the difficulties of testing using APIs that depend upon container support. The intriguing notion of testing servlets by using servlet filters is raised but unfortunately not explored in detail.
Chapter 4. Java EE Testing
Java EE containers supply services without which business code cannot be effectively tested, so how can we test such code? We can try in-container testing, or we can test outside containers, using mocks and stubs to recreate an environment within which the tests can be run without wasting time in deployment and other non-test-oriented activities. Better, we can redesign our code to use delegation from relatively trivial objects dependent upon container interactions to business objects that know nothing of the Java EE APIs surrounding them, merely working with data as it is presented to them, in which case these issues simply vanish. Testing code using a variety of Java EE APIs (JMS, JTA, and so forth) is discussed at length.
Chapter 5. Integration
Integrating TestNG with a number of popular frameworks with useful contributions to make to the testing process is the subject of this chapter. Integration possibilities with Spring, Guice (a newer dependency-injection framework from Google), DbUnit, HtmlUnit, Selenium, and Abbot (for testing Swing code) are discussed and examples are provided. Integration with continuous integration tools is briefly mentioned.
Chapter 6. Extending TestNG
TestNG allows for a considerable amount of customization for those cases where it might not be ideal out-of-box. This chapter covers options like using the TestNG API directly; creating synthetic XML files for runtime, non-static TestNG configuration; scripting; and various options for including and excluding tests as required. The reporting functionality of TestNG is put forward at some length. An intriguing example of creating custom Java annotations to exclude TestNG tests temporarily for a specific period is given.
Chapter 7. Digressions
This is the "odds and ends" chapter of the text. From two regular bloggers, one must perhaps expect these. The proper handling of exceptions (with many anti-patterns); the pitfalls of test-driven development (TDD); the difficulty of testing encapsulated code; the power of debuggers; best practices in logging; and the value of time (with some particular emphases, including the asserted waste in pair programming and the ease with which testing can be overlooked as if it were a time-waster) occupy the attentions of the authors.
Appendices
Appendices explain TestNG IDE integration, the TestNG class structure, the fine points of the main XML configuration file, and migration strategies from JUnit to TestNG.
The web site for the book: http://www.aw-bc.com/catalog/academic/product/0,1144,0321503104,00.html
The TestNG web site is the main source of information about the framework: http://testng.org/doc/index.html
The JUnit web site: http://www.junit.org/
The Spring web site: http://www.springframework.org/
The Guice web site: http://code.google.com/p/google-guice/
The DbUnit web site: http://dbunit.sourceforge.net/
The HtmlUnit web site: http://htmlunit.sourceforge.net/
The Selenium web site: http://www.openqa.org/selenium/
The Abbot web site: http://abbot.sourceforge.net/doc/overview.shtml