Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

TestNG: The next generation of unit testing

Leverage TestNG for unit, synchronous, asynchronous, and parallel testing

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 5

Remember, when using Ant, the default behavior is to use the VM that started Ant to execute all the tasks; in the above scenario, you may well have to use the fork attribute of the Java core task.

Furthermore, multiple instantiations of the TestCase class remain. Surely, if we want the test methods to be independent of each other, we would place them into different TestCase classes. So why multiple instantiations? Multiple instantiations allow you to isolate independent methods within the same TestCase class, which is not always the desired behavior. Finally, too much technical code is required for circumventing the multiple-instantiations scenario: I just want a test POJO (plain old Java object) class.

How does TestNG handle multiple instantiations?
TestNG does not require static block initialization and has a flexible configuration scheme for handling test classes based on regular expressions and XML configuration files. TestNG does not instantiate the test class several times.

Multithreaded support

GroboUtils is an addition to JUnit that supports multithreaded unit tests (since the JUnit framework lacks such support). N. Alex Rupp gives an overview of GroboUtils in "Multithreaded Tests with JUnit" (java.net, August 2003). Although very useful, GroboUtils is not to my taste as it requires too much glue code to handle multithreaded unit tests.

How does TestNG handle multithreaded unit tests?
TestNG has multithreaded and parallel unit tests built in its core. You don't need to write specific code to handle multithreaded unit tests as they are just a configuration of TestNG.

Why should I extend a specific class and prefix the methods with test?

I would rather tag any method than be obliged to prefix methods with test, a task that JUnit requires. Admittedly, this is a minor point, but all IDEs now give you a view (like the Eclipse outline view) that helps you quickly browse your methods to find the one you want—that is, if they are not all prefixed with the same four letters. From a practical viewpoint, the Eclipse outline view is useless when you have dozens of test methods on a class that start with the same four letters.

How does TestNG handle test method names?
TestNG uses Java annotations as specified by Java Specification Request 175 to tag methods for testing and grouping, and for expected exceptions, and so on.

Introducing TestNG

TestNG is inspired by JUnit and also NUnit a unit-testing framework for .Net.

TestNG introduces new functionalities to unit testing such as:

  • Support for Java annotations (if unfamiliar with annotations, see sidebar "What Are Annotations")
  • XML configuration file for test configuration
  • No required class extension or interface implementation
  • Support for dependent methods and groups
  • Support for parallel testing
  • Parameters for test methods
  • Arbitrary number of invocations plus success rate

Step by step: Your first TestNG program

Let's get started with TestNG. The remaining sections of this article introduce you to the TestNG features, starting with a basic first unit test: how to configure it and run it.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources