Categories: testing

TesNG expectedExceptions example

In this post we will learn how to use TestNG expectedExceptions feature to test the expected exception your code might throw in certain situations. We can annotate a test method with particular exception information using @Test(expectedExceptions = Exception.class) if we know that the method will throw this kind of exception during execution. Let’s get going.


Let’s take a simple Calculator class to demonstrate this feature.

package com.websystique.testng;

public class Calculator {

 public int add(int  a, int b){
  return a+b;
 }
 
 public int divide(int a, int b){
  return a/b;
 }

}

We know that divide method will throw an ArithmaticException if second argument is 0.

Let’s write a sample test class to test calculator class.

package com.websystique.testng;

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

public class TestNgExpectedExceptionsExample {

 Calculator calculator;

 @BeforeClass
 public void setup() {
  System.out.println("setup()");
  calculator = new Calculator();
 }

 @AfterClass
 public void tearDown() {
  System.out.println("tearDown()");
  calculator = null;
 }


 @Test
 public void testAdd() {
  System.out.println("testAdd()");
  Assert.assertEquals(calculator.add(3, 4), 7);
 }

 @Test
 public void testDivide() {
  System.out.println("testDivide()");
  Assert.assertEquals(calculator.divide(16, 4), 4);
 }
 
 @Test(expectedExceptions = ArithmeticException.class)
 public void testDivideByZero() {
  System.out.println("testDivideByZero()");
  Assert.assertEquals(calculator.divide(16, 0), 12345);
 }
 

}

testAdd and TestDevide are basic tests. testDevideByZero is special in that it is passing 0 as second argument in calculator.divide() method, which will certainly throw an ArithmeticException. We can specify the particular exception we expect this method to throw using expectedExceptions attribute in @Test annotation like we did @Test(expectedExceptions = ArithmeticException.class).

Run above test class using TestNG Eclipse plugin or maven (mvn clean test). Following is the outcome:

setup()
testAdd()
testDivide()
testDivideByZero()
tearDown()
PASSED: testAdd
PASSED: testDivide
PASSED: testDivideByZero

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

Test testDivideByZero passed successfully despite throwing exception because we informed TestNG that this particular exception is expected behavior.

That’s it.

References

Share
Published by

Recent Posts

Spring Boot + AngularJS + Spring Data + JPA CRUD App Example

In this post we will be developing a full-blown CRUD application using Spring Boot, AngularJS, Spring Data, JPA/Hibernate and MySQL,…

8 years ago

Spring Boot Rest API Example

Spring Boot complements Spring REST support by providing default dependencies/converters out of the box. Writing RESTful services in Spring Boot…

8 years ago

Spring Boot WAR deployment example

Being able to start the application as standalone jar is great, but sometimes it might not be possible to run…

8 years ago

Spring Boot Introduction + hello world example

Spring framework has taken the software development industry by storm. Dependency Injection, rock solid MVC framework, Transaction management, messaging support,…

8 years ago

Secure Spring REST API using OAuth2

Let's secure our Spring REST API using OAuth2 this time, a simple guide showing what is required to secure a…

8 years ago

AngularJS+Spring Security using Basic Authentication

This post shows how an AngularJS application can consume a REST API which is secured with Basic authentication using Spring…

8 years ago