How to test that no exception is thrown?

I know that one way to do it would be:

@Test
public void foo() {
   try {
      // execute code that you expect not to throw Exceptions.
   } catch(Exception e) {
      fail("Should not have thrown any exception");
   }
}

Is there any cleaner way of doing this? (Probably using Junit’s @Rule?)

19 Answers
19

Leave a Comment