TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

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.

TestNG_Exception_Example1

 

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
===============================================

TestNG_Exception_Example2

 

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

Related Posts

Tests TestNG Suite testng.xml

TestNG /

Run tests from TestNG Suite testng.xml file

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

‹ Sample TestNG suite file with Custom Listener› TestNG Test Priority

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version