Search code examples
javajunit

Is there a way to test that a Java method does not throw a specific type of exception?


I am just looking at assertDoesNotThrow() in the JUnit documentation here. I know that you can specify the type of exception with assertThrows() but it appears from the documentation that you cannot do that with assertDoesNotThrow(). Is there a particular reason for that? If you wanted to show that, for example, a method does not throw a ParseException, how would you go about doing that?

Edit: I should have put "does not throw a ParseException when given a particular argument e.g. "hello".


Solution

  • The default for a Java test is that it asserts that the function doesn't throw. You don't need to do any extra work for that; you only need to do extra work to allow it to throw.

    To test that a method does not throw a ParseException, run it. That's all.