Helping ordinary people create extraordinary websites!

TestNG Makes Java Unit Testing a Breeze

By Filippo Diotalevi
2005-03-22

Try this testing framework for its advances over JUnit
The JUnit framework is the current the one-stop solution for Java language unit tests. This framework deserves praise for introducing the idea of test-driven development to Java developers and teaching them how to effectively write unit tests. However, JUnit has only marginally evolved during the last few years; thus, writing tests for today's complex environment has become an increasingly difficult task, in which JUnit must be integrated with several other complementary test frameworks. In this article, Filippo Diotalevi introduces TestNG, a new framework for testing Java applications. TestNG isn't just really powerful, innovative, extensible, and flexible; it also illustrates an interesting application of Java Annotations, a great new feature in JDK 5.0.
In the construction phase of every modern software package, the practice of testing plays a central role. Gone are the days of coding first and testing whenever you have time (or not at all), as most developers now recognize the need to adopt a software methodology in which coding and testing are intermingled and contemporaneous, so as to catch bugs early and identify major risks at the very beginning of the development process.

More than any other testing framework, JUnit has driven developers to understand the usefulness of tests, especially of unit tests. Leveraging a rather simple, pragmatic, and strict architecture, JUnit has been able to "infect" great number of developers. (See Resources for more information on being "test infected.") JUnit users have learned some fundamental rules of unit testing:

• Every single piece of code must be tested.
• Whenever possible, code must be tested in isolation (using, for example, techniques like mock objects).
• Software must be easy testable -- that is, written with tests in mind.

However, as developers' confidence with tests has increased, JUnit's simplicity and strictness has divided them into two opposed factions. On the one hand, there are those who firmly believe that JUnit's simplicity is necessary to continuously remind programmers that software must be simple too (this is known as the KISS principle, for keep it simple, stupid); on the other hand, there are those who, seeing JUnit more as simplistic than simple, want new advanced features, more flexibility, and more power from their testing framework. Some peculiar features of JUnit come in for particular criticism from the latter group:

• The need to extend a TestCase class, because the Java language has single inheritance, is very limiting.
• It is impossible to pass parameters to JUnit's test method as well as to setUp() and tearDown() methods.
• The execution model is a bit strange: The test class is reinstantiated every time a test method is executed.
• The management of different suites of tests in complex projects can be very tricky.

The creator of TestNG
The creator of TestNG is Cedric Beust, a well-known name in the field of Java programming, a member of the EJB 3 expert group, and creator of other popular open source projects like EJBGen and Doclipse. TestNG is distributed under the terms of Apache Software License and can be downloaded from its Web site (see Resources for a link to this and to Cedric's site).
In this article, you will learn how to write unit tests for an application using a new testing framework called TestNG. TestNG takes its inspiration from JUnit, trying to maintain the latter's simplicity; at the same time, however, TestNG eliminates most of the limitations of the older framework and gives the developer the ability to write more flexible and powerful tests. As it heavily borrows from Java Annotations (introduced with JDK 5.0; see Resources for more information on this new feature) to define tests, it can also show you how to use this new feature of the Java language in a real production environment.


Tutorial pages:


First Published on IBM DeveloperWorks


 2 Votes

You might also want to check these out:


Leave a Comment on "TestNG Makes Java Unit Testing a Breeze"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS