Expecting Exceptions Anywhere

Definition:

  • Tests that do not track the step that raised the expected exception and pass

Code Example:

@Test(expected=IndexOutOfBoundsException.class)
    public void testMyList() {
      MyList<Integer> list = new MyList<Integer>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(3);
      list.add(4);
      assertTrue(4 == list.get(4));
      assertTrue(2 == list.get(1));
      assertTrue(3 == list.get(2));
      list.get(6);
  }
  @Test(expected=IndexOutOfBoundsException.class)
    public void testNegative() {
      MyList<Integer> list = new MyList<Integer>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(3);
      list.add(4);
      list.get(-1);
  }
}

References:

Quality attributes

  • - Code Example

  • - Cause and Effect

  • - Frequency

  • - Refactoring