Skip to content

In Java, what are the downsides of Unit test methods having 'throws Exception' clauses on them? #3508

Answered by JojOatXGME
djangofan asked this question in Q&A
Discussion options

You must be logged in to vote

To the best of my knowledge, there is no downside attached to using throws Exception on a test method. JUnit will handle any Throwable thrown by the test method as a test failure. The only exception might be OutOfMemoryError, but this error is also not caught by assertDoesNotThrow. So in general, I would say using throws Exception is completely fine and probably preferable since it is less noisy.

There is a slight difference in that a test failure caused by an AssertionFailedError (which is used by Assertions) might look a bit different from other exceptions. For example IntelliJ will visualize assertion errors in yellow, but other Throwables in red. You can use this to distinguish tests …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by marcphilipp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants