HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
thrownyouhowjunitexceptionassertthatcertaintests

Problem

How can I use JUnit 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);
}


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

Solution

It depends on the JUnit version and what assert libraries you use.

  • For JUnit5 and 4.13 see answer



  • If you use AssertJ or google-truth, see answer



The original answer for JUnit @Test(expected = IndexOutOfBoundsException.class)
public void testIndexOutOfBoundsException() {

ArrayList emptyList = new ArrayList();
Object o = emptyList.get(0);

}
`

Though answer has more options for JUnit

Context

Stack Overflow Q#156503, score: 2584

Revisions (0)

No revisions yet.