Exception Test in TestNG

In this blog post, we are going to learn about one of the important capability of TestNG Framework, and that is expectedExceptions property.

Sometimes, out test method might throw some unhandled exceptions (run-time exceptions), so it is important to handle it through our code, to endure smooth termination of the program.

TestNG provides a mechanism to handle run time exceptions in our code, using expectedExceptions property inside @Test annotation.

Let us understand the capability with working example.

Create a TestNG class, and name it as ExceptionTestTestNG.

package com;

import java.io.IOException;

import org.testng.annotations.Test;

public class ExceptionTestTestNG {

@Test(expectedExceptions = {
IOException.class
})
public void exceptionMethod1() throws Exception {
System.out.println(“Inside Exception Method 1..”);
throw new IOException();
}

@Test(expectedExceptions = {
IOException.class, ArrayIndexOutOfBoundsException.class
})
public void exceptionMethod2() throws Exception {
System.out.println(“Inside Exception Method 2..”);
throw new Exception();
}

}

Run the above TestNG class, and your console window will show something as below.

Inside IO Exception..Inside IO Exception..Inside IO & Null Pointer Exception..PASSED: exceptionTestOneFAILED: exceptionTestTwojava.lang.Exception at com.ExceptionTestTestNG.exceptionTestTwo(ExceptionTestTestNG.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108) at org.testng.internal.Invoker.invokeMethod(Invoker.java:661) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:744) at org.testng.TestRunner.run(TestRunner.java:602) at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at org.testng.TestNG.runSuites(TestNG.java:1144) at org.testng.TestNG.run(TestNG.java:1115) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

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

===============================================Default suiteTotal tests run: 2, Failures: 1, Skips: 0===============================================

If you notice, in the first method (exceptionMethod1), the code throws IOException, and it is handled properly using expectedExceptions property, hence the console does not log failure messages.

On the other hand, in second test method (exceptionMethod2), the code throws Exception, and it is not handled using expectedExceptions property, which results into a run time exception, and console prints a lengthy error log.

To avoid this situations, we should handle the exception(s) that might occur in our code properly.

That is it from this blog post, hope it works for you!

Cheers 🙂

Advertisements