Working with TestNG - Starters Guide

Working with Exceptions

To handle the error occurred during run time(exceptions) TestNG provides an parameter expectedExceptions is used along with the @Test annotations.User can test whether a code throws a desired exception or not. Let's see @Test(expectedExceptions)with an example.

Create a Class:

Create a class named as

package testNG;

public class ExceptionExample {

	public void excepTestExample() {
		System.out.println("Inside excepTestExample()");
		int numb1 = 0;
		int numb2 = 10 / numb1;
		System.out.println("numb2:" + numb2);


Create Test Case Class:

Create a class named as

package testNG;

import org.testng.annotations.Test;

public class TestException {
	ExceptionExample exp = new ExceptionExample();
	 @Test(expectedExceptions = ArithmeticException.class)
	   public void test1() {	
	      System.out.println("Inside test1()");     

Create testing.xml file as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="test Suite">
<test name="Exception Example">
<class name="testNG.TestException"/>

Run the testing.xml file by click on run button in eclipse.

The Output is:

Exception example testNG


This tutorial is focused on getting you started on TestNG, the testing framework inspired from JUnit and NUnit. Here is a quick table of contents

  1. What is TestNG?
  2. Environment Set-up
  3. Writing Tests
  4. Basic Annotations
  5. Execution Procedure
  6. Executing Tests
  7. Suite Test
  8. Ignore Test
  9. Group Test
  10. Exception Test
  11. Dependency Test
  12. Parametrized Test
  13. JUnit Tests
  14. Test Reports
  15. Running tests without Eclipse
  16. Plugin with ANT




A computer capable of running Java. IntelliJ Community IDE is also required for this course.


Good knowledge of Java programming language and eclipse is essential.


Students looking to get started with TestNG

Learning Objectives

This tutorial will get you started with TestNG.

Author: Subject Coach
Added on: 12th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!