How to assert that a certain exception is thrown in jUnit4.5 tests

Posted by SCdF on Stack Overflow See other posts from Stack Overflow or by SCdF
Published on 2008-10-01T06:56:08Z Indexed on 2010/05/29 17:22 UTC
Read the original article Hit count: 456

Filed under:
|

How can use jUnit4.5 idiomatically to test that come code throws an exception?

While I can certainly do something like this:

@Test
public void testFooThrowsIndexOutOfBoundsException() {
  boolean thrown = false;

  try {
    foo.doStuff();
  } catch (IndexOutOfBoundsException e) {
    thrown = true;
  }

  assertTrue(thrown);
}

I recall that there is an annotation or an Assert.xyz or something that is far less cludgy and far more in-the-spirit of jUnit for these sorts of situations.

© Stack Overflow or respective owner

Related posts about java

Related posts about junit