I have a DAO which return some values and how to check a method throws an specific exception?
Asked
Active
Viewed 4,924 times
0
-
possible duplicate of [TestNG: How to test for mandatory exceptions?](http://stackoverflow.com/questions/3677271/testng-how-to-test-for-mandatory-exceptions) – gustafc Jun 13 '13 at 11:42
1 Answers
5
If you're using JUnit and you expect a test to throw a specific exception, do this:
@Test(expected = MyException.class)
public throwsExceptionWhenPassedAnIllegalValue() {
[...]
}
If you're using TestNG, similar syntax:
@Test(expectedExceptions = MyException.class)
public void throwsExceptionWhenPassedAnIllegalValue() {
[...]
}
If the exception that you're expecting is not thrown, these test methods will fail.

stivlo
- 83,644
- 31
- 142
- 199