Site icon TestingDocs.com

TestNG Multiple Exception Tests

Introduction

In this post, we will discuss more details of handling multiple exceptions and other examples with examples. It’s a common misconception that the TestNG framework matches the expected exceptions as a string comparison of Exception names. However, this is not true. We can even specify the superclass of the exception if we are not sure about the exact exception. For example, in the below code we have specified Exception to @Test annotation  “expectedExceptions=”.

Code example

package com.testingdocs.testng.sample;

import java.io.FileNotFoundException;
import org.testng.annotations.Test;

public class TestNGExceptionExample 
{
  
@Test(expectedExceptions = {Exception.class})  
public void exceptionTest() throws FileNotFoundException
  {
  throw new FileNotFoundException() ;
  }
}

 

Output

The test method is marked as passed as shown in the picture. However, if you throw Exception in the test method and expect subclasses in the annotation the test method will fail.

 

Now will see an example if we specify the exception handler in the test method and notice what happens in the below example.

package com.testingdocs.testng.sample;

import java.io.FileNotFoundException;
import org.testng.annotations.Test;

public class TestNGExceptionExample 
{
  
@Test(expectedExceptions = {Exception.class})  
public void exceptionTest() throws FileNotFoundException
  {
  try{
    throw new FileNotFoundException() ;
    }
  catch(FileNotFoundException fnf)
    {
    System.out.println("We are handling FileNotFound and Expecting Exception :-( ");
    }
  }
}

 


Output:

We are handling FileNotFound and Expecting Exception 🙁

FAILED: exceptionTest
org.testng.TestException:

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

 

Multiple Exceptions

TestNG supports multiple expected exceptions in the expectedExceptions for verification in the @Test methods. Specifying multiple exceptions is shown in the below code block:

package com.testingdocs.testng.sample;

import java.io.FileNotFoundException;
import java.io.IOException;
import org.testng.annotations.Test;

public class TestNGExceptionExample 
{
  
@Test(expectedExceptions = {IOException.class,FileNotFoundException.class})  
public void exceptionTest() throws Exception
  {
  throw new IOException() ;		
  }
}

 

IN the above example the thrown exception is checked with the listed of multiple exceptions specified in the expectedExceptions list. The test method will be marked as a failure if the exception is thrown is not on the list. For example, as shown below.

We can also verify the exception message with the TestNG  expectedExceptionsMessageRegExp attribute. If the exception message was specified, its message must match the regular expression specified in this attribute.

package com.testingdocs.testng.sample;

import java.io.FileNotFoundException;
import org.testng.annotations.Test;

public class TestNGExceptionExample 
{
  
@Test(expectedExceptions = {FileNotFoundException.class},
expectedExceptionsMessageRegExp="Exception Message")  
public void exceptionTest() throws FileNotFoundException 
  {
  throw new FileNotFoundException("Exception Message") ;		
  }
}

 

 

In the above example, we have specified the exact message but we can also use regular expressions for matching the exception messages. Practice more test classes in IDE to get the concepts clearer.

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Exit mobile version