-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Prevent usage of @Test(expected = ...) and change existing tests #5221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent usage of @Test(expected = ...) and change existing tests #5221
Conversation
8d0ce4c to
46753c2
Compare
| <module name="RegexpSinglelineJava"> | ||
| <property name="ignoreComments" value="true"/> | ||
| <property name="format" value="@Test\(.*expected.*\)"/> | ||
| <property name="message" value="Prefer using Assertions.assertThatThrownBy(...).isInstanceOf(...) instead."/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually have a helper for this in AssertHelpers
/**
* A convenience method to avoid a large number of @Test(expected=...) tests
* @param message A String message to describe this assertion
* @param expected An Exception class that the Runnable should throw
* @param containedInMessage A String that should be contained by the thrown
* exception's message
* @param runnable A Runnable that is expected to throw the runtime exception
*/
public static void assertThrows(String message,
Class<? extends Exception> expected,
String containedInMessage,
Runnable runnable) {
```There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know but direct Assertions usage gives you much more flexibility around exception verification
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we would probably want to change all the instances in the code base to that then, rather than have two ways to do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like there are about 800 usages of different variations of assertThrows in the codebase, so not sure we'd want to update all of those. Maybe we'll just mention in the checkstyle message Prefer using AssertHelper.assertThrows(...) or Assertions.assertThatThrownBy(...).isInstanceOf(...) instead. I believe both cases should be valid, where Assertions.assertThatThrownBy(...) just gives you more flexibility over things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertThrows was used before we were using Assertions. It's pretty old. I think it's fine to go either way, with a slight preference for using Assertions since it is easier to read. The main thing to watch out for is that the exception message is validated as well as the type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm in favor of steering people towards Assertions usage due to greater flexibility and more fluent assertion checks. In terms of checking the exception message, I think this is a good practice in general, since you really want to make sure you're getting the correct exception with the message you're expecting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my POV, Assertions statements are pretty intuitive to read and interpret and in most cases result in very few lines of code. +1 to promoting them.
We should avoid usage of `@Test(expected = ...)` because it is not always clear from where exactly an exception is thrown. We should rather promote using `Assertions.assertThatThrownBy(...).isInstanceOf(...).hasMessage(...)` as that makes sure that we are in fact getting the right exception with the appropriate error message.
46753c2 to
f34b9a3
Compare
|
Seems like we have general consensus that this is ok to proceed with. Thanks @nastra! |
…che#5221) We should avoid usage of `@Test(expected = ...)` because it is not always clear from where exactly an exception is thrown. We should rather promote using `Assertions.assertThatThrownBy(...).isInstanceOf(...).hasMessage(...)` as that makes sure that we are in fact getting the right exception with the appropriate error message.
We should avoid usage of
@Test(expected = ...)because it is notalways clear from where exactly an exception is thrown. We should rather
promote using
Assertions.assertThatThrownBy(...).isInstanceOf(...).hasMessage(...)as that makes sure that we are in fact getting the right exception with
the appropriate error message.