0

I have a DAO which return some values and how to check a method throws an specific exception?

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
User123456
  • 2,492
  • 3
  • 30
  • 44
  • 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 Answers1

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