Testing that a method throws an exception

Posted by Jens on September 24, 2009

In Java with JUnit 3

  1. public void testIndexOutOfBounds() {
  2.  try {
  3.    new ArrayList().get(0);
  4.    fail("Should have thrown exception");
  5.  } catch (IndexOutOfBoundsException e) {
  6.    assertEquals("Index: 0, Size: 0", e.getMessage());
  7.  }
  8. }
Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

Comments