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
}
}
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!