Tag: assert

java – How do you assert that a certain exception is thrown in JUnit 4 tests?

The Question : 2060 people think this question is useful How can I use JUnit4 idiomatically to test that some 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); }