How can I test if a particular exception is not thrown?

junit

Solution

If you want to test if a particular Exception is not thrown in a condition where other exceptions could be thrown, try this:

try {
  myMethod();
}
catch (ExceptionNotToThrow entt){
  fail("WHOOPS! Threw ExceptionNotToThrow" + entt.toString);
}
catch (Throwable t){
  //do nothing since other exceptions are OK
}
assertTrue(somethingElse);
//done!

Problem

Can I test whether a particular Exception is not thrown? The other way round is easy using `@Test[expect=MyException]`. But how can I negate this?

Original source

Related problems