TestNG – Expected Exception Test

In this tutorial, we will show you how to use the TestNG expectedExceptions to test the expected exception throws in your code.

1. Runtime Exception

This example shows you how to test a runtime exception. If the method divisionWithException () throws a runtime exception – ArithmeticException, it will be passed.

TestRuntime.java

package com.mkyong.testng.examples.exception;

import org.testng.annotations.Test;

public class TestRuntime {

	@Test(expectedExceptions = ArithmeticException.class)
	public void divisionWithException() {
		int i = 1 / 0;
	}

}

The above unit test will be passed.

2. Checked Exception

Review a simple business object, save and update method, and throws custom checked exceptions if error.

OrderBo.java

package com.mkyong.testng.project.order;

public class OrderBo {

  public void save(Order order) throws OrderSaveException {

	if (order == null) {
	  throw new OrderSaveException("Order is empty!");
	}
	// persist it
  }

  public void update(Order order) throws OrderUpdateException, OrderNotFoundException {

	if (order == null) {
	  throw new OrderUpdateException("Order is empty!");
	}

	// If order is not available in the database
	throw new OrderNotFoundException("Order is not exists");

  }
}

Example to test the expected exception.

TestCheckedException.java

package com.mkyong.testng.examples.exception;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.mkyong.testng.project.order.Order;
import com.mkyong.testng.project.order.OrderBo;
import com.mkyong.testng.project.order.OrderNotFoundException;
import com.mkyong.testng.project.order.OrderSaveException;
import com.mkyong.testng.project.order.OrderUpdateException;

public class TestCheckedException {

  OrderBo orderBo;
  Order data;

  @BeforeTest
  void setup() {
	orderBo = new OrderBo();

	data = new Order();
	data.setId(1);
	data.setCreatedBy("mkyong");
  }

  @Test(expectedExceptions = OrderSaveException.class)
  public void throwIfOrderIsNull() throws OrderSaveException {
	orderBo.save(null);
  }

  /*
   * Example : Multiple expected exceptions
   * Test is success if either of the exception is thrown
   */
  @Test(expectedExceptions = { OrderUpdateException.class, OrderNotFoundException.class })
  public void throwIfOrderIsNotExists() throws OrderUpdateException, OrderNotFoundException {
	orderBo.update(data);
  }
	
}

The above unit test will be passed.

Download Source Code

Download it – TestNG-Example-Excepted-Exception.zip (11 kb)

References

  1. TestNG ExpectedExceptions JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
7 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
RyanTestNG Tutorials | TutorialsAnn Barmkyongmkyong Recent comment authors
newest oldest most voted
Ryan
Guest
Ryan

You cannot throw two exceptions in one method. Therefore the second test method will always fail.

trackback
TestNG Tutorials | Tutorials

[…] Tutorial 2 – Expected Exception Test Test the exception throw by the method. […]

Ann Bar
Guest
Ann Bar

I fixed adding the JspException to @Test problem. I am still getting the exceptions.

Ann Bar
Guest
Ann Bar

I fixed my problem with JspException. I am rerunning the tests. Thanks for your help.

Ann Bar
Guest
Ann Bar

Thanks for your reply. Unfortunately I am getting error when I add JspException to @Test.

Ann Bar
Guest
Ann Bar

I was using localhost as my selenium fixture and I was getting a javax.servlet.jsp.JspException: Cannot find bean xxx in scope: “session”. These exceptions flashed through the console but were not there once the test finished running.

mkyong
Guest
mkyong

may be you can narrow down the test cases by using TestNG group feature or enable/ disable feature to find out which unit test causing the JspException.

if your put @Test(JspException) above your unit test method, the exceptions detail will not output to console.

Hope help

Ann Bar
Guest
Ann Bar

What about when your app throws an exception. I see it in my console (eclipse) for a moment and then it is gone. How do I change the configuration so it does show in the console?

mkyong
Guest
mkyong

@Test(expectedExceptions) is used to test the exception from the method,if this method throw an “ArithmeticException” exception, then this unit test is consider passed, because the exception is expected.

May i know what causing you want to display the exception show in console?