Skip to content

Conversation

@nastra
Copy link
Contributor

@nastra nastra commented Jul 7, 2022

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.

<module name="RegexpSinglelineJava">
<property name="ignoreComments" value="true"/>
<property name="format" value="@Test\(.*expected.*\)"/>
<property name="message" value="Prefer using Assertions.assertThatThrownBy(...).isInstanceOf(...) instead."/>
Copy link
Member

@RussellSpitzer RussellSpitzer Jul 11, 2022

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) {
                                  ```

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

@dimas-b dimas-b Jul 13, 2022

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.
@nastra nastra force-pushed the avoid-expected-exception-usage-in-tests branch from 46753c2 to f34b9a3 Compare July 19, 2022 13:20
@danielcweeks
Copy link
Contributor

Seems like we have general consensus that this is ok to proceed with. Thanks @nastra!

@danielcweeks danielcweeks merged commit 21b504c into apache:master Jul 19, 2022
singhpk234 pushed a commit to singhpk234/iceberg that referenced this pull request Jul 20, 2022
…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.
@nastra nastra deleted the avoid-expected-exception-usage-in-tests branch July 20, 2022 06:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants