HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TestNG / TestNG – Expected Exception

TestNG – Expected Exception

With 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.

TestNG provides functionality to test such exception scenarios by allowing the user to specify the type of exceptions that are expected to be thrown by a test during execution.

1. expectedExceptions attribute

Within @Test annotation, TestNG supports multiple exceptions being provided for verification using attribute expectedExceptions. If the exception thrown by the test is not part of the user entered list of exceptions, the test will be marked as failed.

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

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

2. TestNG Expected Exception Tests

In below test, we have two test methods i.e. exceptionTestOne() and exceptionTestTwo().
Here exceptionTestOne() throws IOException where as exceptionTestTwo() throws Exception.

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

public class ExceptionTestDemo 
{
	@Test(expectedExceptions = { IOException.class })
	public void exceptionTestOne() throws Exception {
		throw new IOException();
	}

	@Test(expectedExceptions = { IOException.class, NullPointerException.class })
	public void exceptionTestTwo() throws Exception {
		throw new Exception();
	}
}

Output of above test run is given below:

[TestNG] Running: C:\Users\somepath\testng-customsuite.xml

PASSED: exceptionTestOne
FAILED: exceptionTestTwo

org.testng.TestException: 
Expected exception java.io.IOException but got org.testng.TestException: 
Expected exception java.io.IOException but got java.lang.Exception
	at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1245)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
	at org.testng.TestRunner.privateRun(TestRunner.java:767)
	at org.testng.TestRunner.run(TestRunner.java:617)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
	at org.testng.SuiteRunner.run(SuiteRunner.java:240)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
	at org.testng.TestNG.run(TestNG.java:1057)
	at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.testng.TestException: 
Expected exception java.io.IOException but got java.lang.Exception
	at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:754)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
	... 16 more
Caused by: java.lang.Exception
	at com.howtodoinjava.test.ExceptionTestDemo.exceptionTestTwo(ExceptionTestDemo.java:16)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
	... 18 more

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

As you can see from the test results, exceptionTestTwo() was marked as failed by TestNG during execution. The test failed because the exception thrown by the said method does not match the exception list provided in the expectedExceptions list.

3. Custom Message

We can also verify a test based on the exception message that was thrown by the test. Regular expressions 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 an exception test based on the exception message thrown.

public class ExceptionTestDemo 
{
	@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\somepath\testng-customsuite.xml

PASSED: exceptionTestOne
PASSED: exceptionTestTwo
FAILED: exceptionTestThree

org.testng.TestException: 
Expected exception java.io.IOException but got org.testng.TestException: 
The exception was thrown with the wrong message: expected "Pass Message test" but got "Fail Message test"
	at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1245)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
	at org.testng.TestRunner.privateRun(TestRunner.java:767)
	at org.testng.TestRunner.run(TestRunner.java:617)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
	at org.testng.SuiteRunner.run(SuiteRunner.java:240)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
	at org.testng.TestNG.run(TestNG.java:1057)
	at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.testng.TestException: 
The exception was thrown with the wrong message: expected "Pass Message test" but got "Fail Message test"
	at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1481)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:754)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
	... 16 more
Caused by: java.io.IOException: Fail Message test
	at com.howtodoinjava.test.ExceptionTestDemo.exceptionTestThree(ExceptionTestDemo.java:21)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
	... 18 more


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

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

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

TestNG Tutorial

  • TestNG – Introduction
  • TestNG – Hello World
  • TestNG – Maven
  • TestNG – Annotations
  • TestNG – Expected Exception
  • TestNG – Disable/Ignore Tests
  • TestNG – Parallel Tests
  • TestNG – Dependent Tests
  • TestNG – Timeout Tests
  • TestNG – @Parameters
  • TestNG – @DataProvider
  • TestNG – @Factory
  • TestNG – @DataProvider
  • TestNG – Before and After
  • TestNG – Test Groups

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces