Testing that a method throws an exception
This post is part of the archive. It was written years ago, so some of the tools, versions and links may be outdated.
In Java with JUnit 3
public void testIndexOutOfBounds() {
try {
new ArrayList().get(0);
fail("Should have thrown exception");
} catch (IndexOutOfBoundsException e) {
assertEquals("Index: 0, Size: 0", e.getMessage());
}
}