Exception in TestNG

Exception in TestNG can be described as “Exceptional Event”. An Exception is an event which disrupt the flow of the execution of the application. In TestNG we use expectedException along with @Test annotation to handle the Exception class. We need to specify the exception class name which may occurred at the time of test execution.

Some of the exception class which supported by TestNG are:

  1. IOException.class
  2. NullPoinerException.class
  3. ArithmeticException.class etc.

Example:

Let’s create a Test case to explain the exception briefly.

Create Class:

Create a java class to be tested, say, CheckException.java. Add an error condition inside the displayMessage() method.

public class MessageUtil {

   private String message;

   public MessageUtil(String message) {
      this.message = message; 
   }
   // prints the message
   public void printMessage() {
      System.out.println(message);
      int a =0;
      int b = 1/a;
   }   
   public String salutationMessage() {
      message = "Hi!" + message;
      System.out.println(message);
      return message;
   }   
}  	

Create Test case class:

  • Create a java test class, say, ExpectedExceptionTest.java.
  • Add an expected exception ArithmeticException to the PrintMessage() test case.

Create a java class file named ExpectedExceptionTest.java in same directory.

import org.testng.Assert;
import org.testng.annotations.Test;

public class ExpectedExceptionTest {
   String message = "Pratik";	
   MessageUtil messageUtil = new MessageUtil(message);
	   
   @Test(expectedExceptions = ArithmeticException.class)
   public void PrintMessage() {	
      System.out.println("Inside PrintMessage()");     
      messageUtil.printMessage();     
   }
   
   @Test
   public void SalutationMessage() {
      System.out.println("Inside SalutationMessage()");
      message = "Hi!" + "Pratik";
      Assert.assertEquals(message,messageUtil.salutationMessage());
   }
   @Test
   public void CheckMessage() {
      System.out.println("Inside CheckMessage()");
      message = "Hi!" + "Pratik";
      Assert.assertEquals(message,messageUtil.salutationMessage());
   }
}

Create Test Runner:

Make testng.xml in C:\>TestNG_WORKSPACE to execute test case(s).

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "ExpectedExceptionTest" />
      </classes>
   </test>
</suite>

Now you need to compile MessageUtil Test case classes using javac.

C:\TestNG_WORKSPACE>javac MessageUtil.java TestJunit.java.

C:\ >java -cp “C:\ ” org.testng.TestNG testng.xml.

Verify the output. PrintMessage() test case will be passed.

Inside testPrintMessage()

Pratik

Inside testSalutationMessage()

Hi!Pratik

===============================================

===============================================

From the above example, we got the clear picture of the exception and how to handle exception in TestNG. For this we need 3 class. Test class, check exception class and Test Runner class.

In ExpectedExceptionTest class, we will create our test case and define the function in which we with the help of assertequals we will verify the message.

Test Runner class is nothing but a configuration class in which we will define the suite name, test name, class name etc. In class name we ill mention the class name which we want to execute for the exception. If we want to run more than one test case class to execute the exception, then we will define the the class name in separate class name tag.

Leave a Reply

Your email address will not be published. Required fields are marked *