TestNG Exception | TestNG Tutorial

TestNG Exception:

In this post, I will show how to use TestNG Exception (i.e., expectedExceptions) parameter along with the @Test annotation. TestNG provides an option of tracing the exception handling of code.

Firstly, we see a basic program without using TestNG Exception.

Created a java class, say, TestNGException.java and added an error condition in the method testException().

testng.xml:

Console Output:

As you can see from the test results, testException() method was marked as failed by TestNG during execution.

Now let’s see TestNG Exception in action.

Check below video to see “TestNG Exceptions”

Please be patient. The video will load in some time.

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

The expected exception to validate while running the below test is mentioned using the expectedExceptions attribute value while using the @Test annotation.

testng.xml:

Console Output:

As you can see from the test results, testException() method was marked as passed by TestNG during execution.

You could find the complete TestNG tutorial here.

TestNG Complete Tutorial

If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.

TestNG Exception

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

Leave a Comment

Share via
Copy link
Powered by Social Snap