Expected Exception and Expected Message in TestNG

Expected Exception and Expected Message in TestNG

While writing unit tests there can be certain scenarios where we need to verify that an exception is being thrown by the program during execution or not. TestNG provides a feature to test such scenarios by allowing the user to specify the type of exceptions that are expected to be thrown by a test method during execution.

  • It supports multiple values being provided for verification.
  • If the exception thrown by the test is not part of the user entered list, the test method will be marked as failed.

Let’s create a sample test and learn how exception test works in TestNG.

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

Let’s see an example to understand it better.

In below test, we have two test methods i.e. TC001() and TC002().In both the methods it will throws an exception called “ArithmeticException” when dividing two numbers with denominator value as 0.

package TestNGException;

import java.io.IOException;

import org.testng.annotations.Test;

public class TestNGException {@Test(expectedExceptions = ArithmeticException.class)
	public void TC001() {
		int e = 1 / 0;
	}

	@Test(expectedExceptions = {
		IOException.class,
		NullPointerException.class
	})
	public void TC002() {
		int e = 1 / 0;
	}
}

 Output of above test run is given below:

[TestNG] Running:
C:\Users\Sujoy\AppData\Local\Temp\testng-eclipse-1037333163\testng-customsuite.xml
PASSED: TC001
FAILED: TC002

When we execute the above code, the test method “TC001” will return as “Passed” as we are handling the exceptions and the Test Method “TC002” will return output as “Failed” with exception as ‘“java.lang.ArithmeticException: / by zero”

Example of Expected Exception Test with Verifying Message

You can also verify a test based on the exception message that was thrown by the test. Regular expression can also be used to verify the error message, this can be done using .*. Depending upon the position of the regular expression we can use it to do pattern matching such as starts-with, contains, and ends-with while verifying the exception message.

Let’s learn how to write a exception test based on the exception message thrown.

package TestNGException;

import java.io.IOException;

import org.testng.annotations.Test;

public class TestNGException {@Test(expectedExceptions = {
		IOException.class
	},
	expectedExceptionsMessageRegExp = "Pass Message test")
	public void exceptionTestOne() throws Exception {
		throw new IOException("Pass Message test");
	}

	@Test(expectedExceptions = {
		IOException.class
	},
	expectedExceptionsMessageRegExp = ".* Message .*")
	public void exceptionTestTwo() throws Exception {
		throw new IOException("Pass Message test");
	}

	@Test(expectedExceptions = {
		IOException.class
	},
	expectedExceptionsMessageRegExp = "Pass Message test")
	public void exceptionTestThree() throws Exception {
		throw new IOException("Fail Message test");
	}

}

Output of above test run is given below:

[TestNG] Running:
  C:\Users\Sujoy\AppData\Local\Temp\testng-eclipse–1391524594\testng-customsuite.xml
PASSED: exceptionTestOne
PASSED: exceptionTestTwo
FAILED: exceptionTestThree

In above test methods exceptionTestThree() failed because expected message didn’t matched.

Leave a Reply

Your email address will not be published. Required fields are marked *