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

TestNG Makes Java Unit Testing a Breeze

By Filippo Diotalevi
2005-03-22


Exception checking

With TestNG, you can check the occurrence of an exception very simply and easily. It is obviously possible to do this with JUnit as well, but using the @ExpectedExceptions annotation with TestNG makes the code of the test surprisingly easy and straightforward to write, as you can see in the example presented in Listing 6. The @ExpectedExceptions annotation specifies that the raising of a NumberFormatException is tolerated by the framework and therefore should not be considered a failure. To see if an exception is raised in a certain line of code, you can add an assert false statement just after that line. This means that you will pass the test only if that particular type of exception is raised in the designated line.

Listing 6. Exception checking with TestNG
public class  NumberUtilsTest

{
@Test(groups = {"tests.math"})
@ExpectedExceptions(NumberFormatException.class)
public void test()
{

NumberUtils.createDouble("12.23.45");
assert false; //shouldn't be invoked
}

}


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