Helping ordinary people create extraordinary websites!

TestNG Makes Java Unit Testing a Breeze

By Filippo Diotalevi
2005-03-22

Configuration methods
With TestNG, you can specify more than just test methods; it's also possible to specify other particular methods in a class, called configuration methods, using the dedicated annotation @Configuration. There are four types of configuration methods:

• beforeTestClass methods are executed after the instantiation of the class but before any test method has been run
• afterTestClass methods are executed after every test method in a class has been run
• beforeTestMethod methods are executed before the execution of any test method in a class
• afterTestMethod methods are executed after the execution of every test method in a class

The lifecycle of a test class is further illustrated in Figure 2.

Figure 2. Lifecycle of a test class


Listing 5 illustrates some examples of configuration methods. Please note that if you use groups, configuration methods must also belong to a group. Furthermore, the four types of configuration methods are not mutually exclusive, so it is possible to define methods that belong to one or more of these categories at the same time (see the aroundTestMethods() method in Listing 5 for an example).

Listing 5. Examples of configuration methods
 @Configuration(beforeTestClass = true, groups = {"tests.workflow"})

public void setUp()
{
System.out.println("Initializing...");
}

@Configuration(afterTestMethod = true, beforeTestMethod = true, groups = {"tests.workflow"})
public void aroundTestMethods()
{

System.out.println("Around Test");
}


Configuration methods in TestNG are a more powerful version of JUnit's setUp() and tearDown() methods; their main purpose is to create the right execution context for a test and to refresh data after the execution of a test case.



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