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

If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help me improve further our learning process.

If you appreciate the effort I have put in this learning site, help me improve the visibility of this site towards global audience by sharing and linking this site from within and beyond your network. You & your friends can always link my site from your site on www.websystique.com, and share the learning.

After all, we are here to learn together, aren’t we?