TestNG Tutorial 2 – Expected Exception Test
Published: May 10, 2009 , Updated: April 1, 2010 , Author: mkyong
It’s used to test the exception throw by the method.
import org.testng.annotations.*; /** * TestNG Expected Exception Test * @author mkyong * */ public class TestNGTest2 { @Test(expectedExceptions = ArithmeticException.class) public void divisionWithException() { int i = 1/0; } }
In above example, the divisionWithException() method will throw an ArithmeticException Exception, since this is an expected exception, so the unit test will pass.
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[...] Tutorial 2 – Expected Exception Test Test the exception throw by the method. [...]
I fixed adding the JspException to @Test problem. I am still getting the exceptions.
I fixed my problem with JspException. I am rerunning the tests. Thanks for your help.
may be you can share your solution here?
Thanks for your reply. Unfortunately I am getting error when I add JspException to @Test.
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.
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
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?
@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?