TestNG expectedExceptions Attribute

TestNG expectedExceptions Attribute: In this post, we will understand how to use the testNG exception (expectedExceptions) with the @test method. TestNg provides a feature where a user can specify the type of exception expected to be thrown by a test method during execution.

Users can also mention multiple values. If the exception thrown by the test is not a part of the user-entered list, the test method will be marked as failed.

It is more important to test an application’s positive and negative behavior because if you do not deal with such a situation, your application has to be terminated. So, in this case, you need to use these expectedExceptions so that your program will run smoothly.

Syntax:

Using expectedExceptions, we can handle any exception in a testNg program. We have to mention the type of exception with the .class.

expectedExceptions Attribute Example

@Test(expectedExceptions = { ArithmeticException.class })
@Test ( expectedExceptions = { IOException.class, NullPointerException.class } )

Let us try to understand the above concept with a simple example:

package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class ExpectedExceptionsEx1 
{
	@Test
	public void testException() 
	{
		System.out.println("SoftwareTestingMaterial.com");
		// This Will Throw / by zero ArithmeticException
		int i = 1 / 0;
	}
}

Output:

www.softwaretestingo.com
FAILED: com.softwaretestingo.testng.attributes.ExpectedExceptionsEx1.testException
java.lang.ArithmeticException: / by zero
===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================

If you execute the above script, you can notice that in the above testNG class, we have not used the expectedExceptions in the program. That is why the test method failed, but let us update the same class with the expectedExceptions attribute to run the same and see the output.

After modifying the class:

package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class ExpectedExceptionsEx1 
{
	@Test(expectedExceptions = ArithmeticException.class)
	public void testException() 
	{
		System.out.println("www.softwaretestingo.com");
		int i = 1 / 0;
	}
}

Now, when we run the above TestNG class.

www.softwaretestingo.com
PASSED: com.softwaretestingo.testng.attributes.ExpectedExceptionsEx1.testException
===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

If the expected and throwing exceptions are not matched, you will get a TestException. Let us take an example:

package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class ExpectedExceptionsEx1 
{
	@Test(expectedExceptions = {ArithmeticException.class})
	public void testException() 
	{
		System.out.println("www.softwaretestingo.com");

		int arr[] = null; // array is assigned a null value
		System.out.println("The length of the array arr is: " + arr.length);
	}
}

Output:

www.softwaretestingo.com
FAILED: com.softwaretestingo.testng.attributes.ExpectedExceptionsEx1.testException
org.testng.TestException
Expected exception of type class java.lang.ArithmeticException but got java.lang.NullPointerException: Cannot read the array length because "arr" is null

Example Of Multiple ExpectedException declaring

package com.softwaretestingo.testng.attributes;
import org.openqa.selenium.NoSuchElementException;
import org.testng.annotations.Test;
public class ExpectedExceptionsEx3 
{
	@Test(expectedExceptions = {NullPointerException.class,NoSuchElementException.class,ArithmeticException.class})
	public void testException() 
	{
		System.out.println("www.softwaretestingo.com");

		int i = 1 / 0;
	}
}

Output:

www.softwaretestingo.com
PASSED: com.softwaretestingo.testng.attributes.ExpectedExceptionsEx3.testException

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

When we used the expectedExceptions attribute with the @test annotation method. It will compare the expectedExceptions attribute value with the result exception. If both the values match, it will be handled by TestNg.

That means your @Test method will be passed, and the remaining part of that method will be skipped.

You can get more TestNg tutorials by following the link; if you have any suggestions, let us know in the comment section.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment