Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

TestNG Makes Java Unit Testing a Breeze

By Filippo Diotalevi
2005-03-22


TestNG quickstart

A TestNG test class is a plain old Java object; you don't need to extend any special class or use any naming convention for test methods: You just use the annotation @Test to signal to the framework the methods of the class that are tests. Listing 1 illustrates one of the simplest tests possible for the utility class StringUtils. It tests two methods of StringUtils: isEmpty(), which checks a String for emptiness, and trim(), which removes control characters from both ends of a String. Note that the assert Java instruction is used to check error conditions.

Listing 1. A test case for class StringUtils
package tests;


import com.beust.testng.annotations.*;
import org.apache.commons.lang.StringUtils;

public class StringUtilsTest
{
@Test
public void isEmpty()
{
assert StringUtils.isBlank(null);
assert StringUtils.isBlank("");
}

@Test
public void trim()
{
assert "foo".equals(StringUtils.trim(" foo "));
}
}


Before you can run the tests, however, you must configure TestNG using a special XML file, conventionally named testng.xml. The syntax for this file is very simple, and is presented in Listing 2. This file begins by defining a test suite, My test suite, composed of a unique test, First test, that is made by the StringUtilsTest class.

Listing 2. Configuration file for TestNG
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >

<suite name="My test suite">
<test name="First test">
<classes>
<class name="tests.StringUtilsTest" />
</classes>
</test>
</suite>


If this sample testng.xml file doesn't seem very useful (there's only one test class), the great news is that it is actually the only file you need to write to define your test suites. Remember the old days of JUnit? In those days, the definition of your suites was probably spread over several files: JUnit's TestSuites, property files, and, obviously, Ant build files. With TestNG, all the data you need is gathered in the testng.xml file. No more TestSuites, and a thinner build file.

To run the test, you compile the class with javac and then invoke TestNG with the following command:

java -ea -classpath .;testng.jar;commons-lang-2.0.jar com.beust.testng.TestNG testng.xml

Here, the option -ea tells the JVM to handle assertions (and to raise an exception when an assertion fails); testng.jar and commons-lang-2.0.jar are the only two libraries required to run this example, and com.beust.testng.TestNG is the main class of TestNG. For all you lazy developers who have happily forgotten the cryptic syntax of java and javac, a useful Ant task is also available. Listing 3 presents, as an example, the Ant build file of the sample application distributed with this article. Note the definition of the testng task associated with the class com.beust.testng.TestNGAntTask, and its rather simple usage in the test target.

Listing 3. Ant build file with TestNG task
<project name="sample" default="test" basedir=".">


<!-- COMPILE TESTS-->
<path id="cpath">
<pathelement location="testng.jar"/>
<pathelement location="commons-lang-2.0.jar"/>
</path>
<target name="compile">
<echo message="compiling tests"/>
<mkdir dir="classes"/>
<javac debug="true"
source="1.5" classpathref="cpath"
srcdir="src" destdir="classes"/>
</target>

<!-- RUN TESTS-->
<taskdef name="testng"
classname="com.beust.testng.TestNGAntTask"
classpathref="cpath"/>
<path id="runpath">
<path refid="cpath"/>
<pathelement location="classes"/>
</path>
<target name="test" depends="compile">
<echo message="running tests"/>
<testng fork="yes" classpathref="runpath" outputDir="test-output">
<fileset dir="src" includes="testng.xml"/>
<jvmarg value="-ea" />
</testng>
</target>

</project>


If all has been done correctly, you should see the results of your tests in the console. Furthermore, TestNG creates a very nice HTML report in a folder called test-output that is automatically created in the current directory. If you open it and load index.html, you will see a page similar to the one in Figure 1.

Figure 1. HTML report created by TestNG


Tutorial Pages:
» Try this testing framework for its advances over JUnit
» About the code
» TestNG quickstart
» Defining test groups
» Configuration methods
» Exception checking
» Wrapping up
» Resources


First Published on IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources