Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 14, 2016

TestNG Exception Test





While writing automation tests there can be certain scenarios where we need to verify that an exception is being thrown by the program during execution. 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.

Will see a sample programs to test the expected exceptions:

Scenario 1: Here we will NOT handle the exception and will see the result:


import org.testng.annotations.Test;

public class ExceptionTest
{
    @Test()
    public void exceptionTestOne()
    {
        int i = 1/0;
        System.out.println("Value of i :" + i);
    }
}

Now will execute the above program and see the result:

FailedTestDueToException

In the above program, the output is Failed and it will throw Arithmetic Exception as we can not divide anything by ZERO and it is not handled in the program.

Scenario 2: Here we will handle the exception and will see the result:

import org.testng.annotations.Test;

public class ExceptionTest
{
    @Test(expectedExceptions={ArithmeticException.class})
    public void exceptionTesting()
    {
        int i = 1/0;
        System.out.println("Value of i :" + i);
    }
}

Now will execute the above program and see the result:

PassedTestDueToExceptionHandling

In the above program the output is Passed and it will NOT throw any exception as we  have handled the exception using expectedExceptions Test annotation attribute as we know that there may be a chance of failing the program because of divide by ZERO.

In this way we will handle the exceptions using expectedExceptions Test annotation attribute.



Share this post: on Twitter on Facebook

TestNG Test Annotation TestNG Time-Out Test

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025