September 24, 2009

Testing that a method throws an exception

0  comments

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());
 }
}


Tags

Java, Junit, Snippets, TDD


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

Information about Data protection

This site uses Akismet to reduce spam. Learn how your comment data is processed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}